If I would like to call api when Fragment onPause.
Is it the case to use
GlobalScope.launch
?
Copy code
// Fragment
override fun onPause() {
viewModel.leave()
}
Copy code
// ViewModel
fun leave() {
GlobalScope.launch {
repository.leave()
}
}
e
Evilthreads669966
10/26/2020, 10:57 PM
I would turn leave into a suspension function. And then you could say mainScope.launch and then withContext(Dispatchers.IO) but it would be done better using lifecycleowner
Evilthreads669966
10/26/2020, 10:59 PM
Inside of your suspension function you could do withContext
s
Simon Lin
10/28/2020, 7:42 AM
Does the api get cancel when it bound to lifecycleowner?
I would like to call api when view destroying. But the coroutine will cancel all api when the view destroy.