Such a question - I need to send http requests, bu...
# android
d
Such a question - I need to send http requests, but I need to bind not to the activity lifecycle (in my case, viewModelScope), but to the life cycle of the app itself) I don’t really want to use the service for this. Why is GlobalScope bad in my case?
1
m
Google's Manuel Vivo wrote https://medium.com/androiddevelopers/coroutines-patterns-for-work-that-shouldnt-be-cancelled-e26c40f142ad that covers it. His recommendation is a custom application-level
CoroutineScope
.
d
Thanks!
o
Its also as simple as just adding the scope to either your application class, or if you're using DI like dagger, just create a provides, Singleton method and in both cases you just need the following:
CoroutineScope(SupervisorJob() + Dispatchers.Main)
Then in all places where you need access to this scope you either just inject it into the constructor of that class (try to avoid field injection) or you just go through the application context to get the reference.
o
@OG wouldn't a MainScope() would be the same?
o
@Orhan Tozan yes it is. However there are no optional params you can pass to that function/constructor, so you will still want to create your own CoroutineScope like above to pass in extra flags/customization for your scope, via the CoroutineContext param.