https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

Seri

07/28/2019, 10:49 PM
On Android, what CoroutineContext would y’all use for a fire-and-forget network request where the app doesn’t care about the response? Is this a valid case for
GlobalScope.launch { ... }
?
👍 1
l

louiscad

07/28/2019, 10:52 PM
GlobalScope.launch(Dispatchers.Main)
if I have to be in-sync with stuff on main thread
Or I use this scope:
val AppProcessScope = CoroutineScope(Dispatchers.Main)
But I tend to avoid non cancellable scopes and tie them to a lifecycle, or cancel them at some point based on other things.
2
s

Seri

07/29/2019, 12:13 AM
Gotcha, thanks. This is just an HTTP post call that has no bearing on the UI
l

louiscad

07/29/2019, 12:51 AM
Can you tell what is its purpose?
j

Jag

07/29/2019, 5:12 AM
I would recommend something like what Louis suggested. Having explicit control will prove to be valuable with little upfront investment. Personally, I use an AppScope and UserSessionScope.
👍 1
s

Seri

07/29/2019, 4:04 PM
@louiscad I’m just throwing a piece of data at the server for it to store. I don’t want it to be cancelled if the user leaves the screen, so I’d like for it to be tied to the application scope. An AppScope or UserSessionScope would be perfect for this, but it’d be a large investment to implement in our project
l

louiscad

07/29/2019, 4:05 PM
@Seri I'd use WorkManager for this kind of thing to handle failures and retries correctly. The ktx artifact integrates nicely with coroutines.
s

Seri

07/29/2019, 4:06 PM
Nice! I’ll take a look
t

Thomas

07/30/2019, 6:06 PM
Or I use this scope:
val AppProcessScope = CoroutineScope(Dispatchers.Main)
@louiscad I assume you put this in your Android Application class? What is the benefit using this over the already provided
GlobalScope
?
l

louiscad

07/30/2019, 10:30 PM
@Thomas I'd put it anywhere. GlobalScope doesn't specify a dispatcher, so it defaults to the multithreaded
Dispatchers.Default
.
👍 1
4 Views