spierce7
05/18/2021, 4:18 PMGlobalScope
delicate api legitimate use cases.
It’s my understanding that any time I would create a generic CoroutineScope
, and then not cancel it, I can use GlobalScope
instead. Is that correct? The main hesitation with the GlobalScope
is simply that you can’t cancel it, and thus, it doesn’t lend itself well to hierarchical coroutines, right?.Zach Klippenstein (he/him) [MOD]
05/18/2021, 4:31 PMspierce7
05/18/2021, 4:35 PMPushMessageRepository
. I don’t want to cancel the handling of the push message under any circumstances (I don’t think?).
I would normally call:
GlobalScope.launch {
pushMessageRepository.handleMessage(message)
}
Instead, what should I do, and how does it produce different results?streetsofboston
05/18/2021, 5:15 PMZach Klippenstein (he/him) [MOD]
05/18/2021, 5:22 PMsteamstreet
05/18/2021, 5:30 PMstreetsofboston
05/18/2021, 5:34 PMsteamstreet
05/18/2021, 5:36 PMsteamstreet
05/18/2021, 5:37 PMGlobalScope.launch { callback() }
steamstreet
05/18/2021, 5:38 PMGlobalScope.launch
spierce7
05/18/2021, 6:02 PMBecause as soon as you want to customize the dispatcher, or any other aspect of the default context for your global scope, you’ll just create a custom scope anywayBut when you are using a member variable, you have to remember to create it as a
SupervisorJob
, as Job
isn’t what you actually want most of the time in those cases.spierce7
05/18/2021, 6:03 PMGlobalScope + Dispatchers.Main
. This isn’t a performance thing, it’s a convenience thing.