Maciek
04/16/2020, 4:33 PMZach Klippenstein (he/him) [MOD]
04/16/2020, 4:37 PMCoroutineScope
factory function then yes, you'd need a new scope.Maciek
04/16/2020, 4:53 PMCoroutineScope
section 😄 For factory function are you referring functions like MainScope()
? I'm using them but still keeping the object in the field for reuse, is this something that should be avoided?Zach Klippenstein (he/him) [MOD]
04/16/2020, 4:55 PMval scope: CoroutineScope = CoroutineScope(yourContext)
factory function.Zach Klippenstein (he/him) [MOD]
04/16/2020, 5:21 PMMainScope()
is closely related to. Keeping scopes in a field like that is fine, even encouraged.Maciek
04/16/2020, 6:14 PMval scope
in activity or fragment, at least when you want to cancel coroutines on pause or on stop. If the system will use same activity object to go back to on start and on pause then your coroutines will just silently not run 🙂Zach Klippenstein (he/him) [MOD]
04/16/2020, 6:22 PMMarcelo Hernandez
04/17/2020, 3:06 AMscope
in onResume()
, then you can instead do scope.coroutineContext.cancelChildren()
in onPause()
. This keeps the scope
active for reuse but still makes sure to cancel any launched coroutines.Marcelo Hernandez
04/17/2020, 3:07 AMscope.cancel()
<-> compositeDisposable.dispose()
scope.coroutineContext.cancelChildren()
<-> compositeDisposable.clear()
Maciek
04/17/2020, 4:31 AMaipok
04/17/2020, 6:22 AMviewLifecycleOwner.lifecycleScope.launchWhenResumed {
someWorkThatOnlyPerformedWHileResumed
}
That method will stop coroutine in case app is on pause and will cancel it properly in case activity is destroyed.dekans
04/17/2020, 7:05 AMaipok
04/17/2020, 7:28 AMdekans
04/17/2020, 8:17 AMscope.cancelChildren()
It's dangerous if you use channels/actors they will be closed, you'd have to reinstanciate them