https://kotlinlang.org logo
#coroutines
Title
# coroutines
o

oday

08/30/2018, 7:05 PM
by the time that this code reaches the oncomplete listener, requests to an API which use the
carsService
object have already went out
ok, this thread
and when I do this without having a completeListener, I get back fgrom each of those requessts that Task is not yet complete (referring to the task of getting the token ID
(sorry, mac keyboard)
d

diesieben07

08/30/2018, 7:07 PM
So, the idea is that you write a
suspend
extension function on whatever you need to "wait for using a callback". In this case that would be whatever
getIdToken
returns
o

oday

08/30/2018, 7:07 PM
alright, and what does that suspend
d

diesieben07

08/30/2018, 7:07 PM
sorry, mac keyboard
My sincerest condolences
o

oday

08/30/2018, 7:07 PM
my whole program?
i just wanna buy a second
d

diesieben07

08/30/2018, 7:08 PM
No, the current coroutine. I see that in your code you are doing this inside an
init
block, that cannot work.
o

oday

08/30/2018, 7:08 PM
yes
d

diesieben07

08/30/2018, 7:08 PM
You can't suspend in an init block or a constructor
o

oday

08/30/2018, 7:08 PM
aha
this init block initializes the object on which other dependent api requests..depend on
hope that’s clear
cause this whole object is provided through DI
d

diesieben07

08/30/2018, 7:08 PM
Yes, I know what an init block does 😛
But may I suggest using a factory method instead and doing the API requests there?
Generally constructors should be "state-less" (I don't think that's the right word)
o

oday

08/30/2018, 7:09 PM
well the place where those requests come out is inside an Activity (pure UI)
d

diesieben07

08/30/2018, 7:10 PM
Don't tell me you are doing requests on the UI thread...?
o

oday

08/30/2018, 7:11 PM
no, i just call the presenter, the presenter calls the repository implementation class (which is provided by dagger to that activity (which then passes it to the presenter))
so inside the repository implementation , the service is initialized with the HTTP client, where the firebase token is grabbed and added to the header
that service and all its calls go out in observables
d

diesieben07

08/30/2018, 7:12 PM
Okay. Can we clarify what your primary problem is now?
o

oday

08/30/2018, 7:12 PM
that the requests when they go out, the repository initialization code is not yet done with getting that token
so they fire before
actually you see in the paste i sent
d

diesieben07

08/30/2018, 7:13 PM
Okay, so you want your
init{}
to block until that listener is done?
o

oday

08/30/2018, 7:14 PM
hmmm
let me fix that paste
one second
ok I solved it
I simply should not be making this call at each time the headers are being prepared for an API request
simply
I just refresh this token at a point where I can, and then in the oncomplete listener i save that token for the rest of the usage throughout the program
d

diesieben07

08/30/2018, 7:25 PM
Well, I'm glad you solved it 😄
o

oday

08/30/2018, 7:25 PM
i dont ask for it at every single request
crises averted
thanks man