by the time that this code reaches the oncomplete ...
# coroutines
o
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
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
alright, and what does that suspend
d
sorry, mac keyboard
My sincerest condolences
o
my whole program?
i just wanna buy a second
d
No, the current coroutine. I see that in your code you are doing this inside an
init
block, that cannot work.
o
yes
d
You can't suspend in an init block or a constructor
o
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
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
well the place where those requests come out is inside an Activity (pure UI)
d
Don't tell me you are doing requests on the UI thread...?
o
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
Okay. Can we clarify what your primary problem is now?
o
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
Okay, so you want your
init{}
to block until that listener is done?
o
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
Well, I'm glad you solved it đŸ˜„
o
i dont ask for it at every single request
crises averted
thanks man