Hiya, on my splash screen (android) I want to fetc...
# coroutines
l
Hiya, on my splash screen (android) I want to fetch some remote config in the background, then only move on from the screen once the fetch has completed (either successfully or via exception, I don’t care) I have a private field that should launch immediately upon the class being instantiated
Copy code
private val remoteConfigFetch = lifecycleScope.launch { FirebaseRemoteConfig.init() }
Then further on before the splash screen finally moves on I’m calling join
Copy code
private fun continueToApp() = runBlocking {
    remoteConfigFetch.join()
    goToMainActivity()
}
Is this the correct approach? Initially I thought about using async but figured because I don’t care about whether it fails or errors and don’t care about the result it was better to use launch?
a
I don't think you'd use
runBlocking
here, from where ever you're calling this that thread would become blocked, I suggest to use suspend modifier in
continueToApp
instead of using runBlocking{} In launching part everything looks promising, but if your call requires some i/o or networking then be sure to wrap the call under withContext(Dispatchers.IO){"code"} inside launch.
l
If I made the continueToApp function a suspend function then I would have to call it from a coroutine. I'm launching the config coroutine from a field directly so I would have to add another coroutine and then every chain up the call stack would also have to become suspend functions, doesn't seem ideal
a
If that's the case launching part is at its best optimization! From which thread you're calling continueToApp from?
l
Main thread, everything is on the main thread except for the code inside launch
a
A big NO, you could have call that continueToApp by launching a coroutine, even you could use Dispatchers.Main but don't use runBlocking, it won't suspend until join() it will instead block until join() has completed and goToMainActivity has finished.
Donot mess up with Main and UI thread it is responsible for responsiveness, if you start blocking them youre app become unresponsive and if user won't be able to interact with his device untill android detect that app is unresponsive and wish to kill or wait fir response. Always use suspension where ever it is supposed to.
l
I can’t see how there would be a difference in using runBlocking vs someScope.launch there. It’s on the splash screen so there is no user interaction. This is the last piece of code on that screen so I need to make sure that the remote config has finished loading before proceeding to the next screen