I want to clarify one doubt regarding Kotlin Coroutines.
How can I delay the execution of the next line or next block of code in my app until the response of my ongoing API comes?
For e.g. Suppose I want to call one API when a user opens the app and only after I get the response from that API I want to draw the views or anything else. It can be as simple as log anything or show toast or something like that.
Note: Without using runBlocking{ } or delay() methods.
For a simple tasks you could use invokeOnCompletion which will execute only after the coroutine finish it's job.
Here's a little example
lifecycleScope.launch(Dispatchers.IO) {
//heavy calculations
}.invokeOnCompletion {
Toast.makeText(requireContext(), "Coroutine ended it's job", Toast.LENGTH_SHORT).show()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With