Hi everyone, we have a LifecycleObserver, which ha...
# android
l
Hi everyone, we have a LifecycleObserver, which has an onCreate and onStart function. Both functions call suspend functions, so we are passing a CoroutineScope to the LifecycleObserver Is there any way we can guarantee that the suspend onCreate function runs to completion before the onStart? Do we need to use runBlocking?
r
not entirely sure, but if you create new `coroutineScope`s in
onCreate
and
onStart
as children of the passed scope, they should run sequentially
g
There is no such guarantee, I would even say that onStart it most probably will run earlier
What is your use case for it?
Essentially run blocking is the only way to do this, to block main thread, but I think it's obviously dangerous, even if you expect that suspend function will complete fast
l
In onCreate we are clearing a session ID, in onStart we are fetching the session ID or creating a new one if it has been cleared Basically if the app process has been killed we want to start a new session If the app process is still alive and the user is just returning to the app then use the existing session
g
But why do you start new session on activity instead of application?
Anyway, there is no way to do this without blocking Probably always retrieve session asynchronously is better solution, thought it of course may require much more changes in code which depends on it
l
This is all in Application, not activity
👌 1
g
runBlocking or fully asynchronous api to access session is way to go
t
I'd try with creating CompletableDeferred in onCreate and await on it in onStart. Once the onCreate is complete, complete the deferred.
g
But it would be still run blocking to await it, isn't?