Such a question - I need to send http requests, bu...
# android
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?
Google's Manuel Vivo wrote that covers it. His recommendation is a custom application-level
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.
@OG wouldn't a MainScope() would be the same?
@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.