suspend fun thing() {
with (dispatchers.io) {}
}
Dispatchers:
- Main
- IO (network/disk)
- Default (CPU bound)
Good suspend functions are "main safe"
they had literally less ram than i have fingers
When a suspend function returns, it has completed all work.
launch { }
coroutine builder
- What cancels coroutine work
- Who gets the exceptions
Scopes keep track of coroutines
val scope = CoroutineScope(Dispatchers.Main) // MainScope()
scope.launch { ... }
ViewModel.viewModelScope
coroutine scope for android viewmodels (in alpha)
launch
does not automatically start in the caller's scope, coroutineScope {}
builder will do that though.
-
creates child scope of the caller's scope
-
Only resumes after children completes
-
scope returns when child work is complete
-
scope is cancelled when children are cancelled
-
scope is notified when children error
scope.async {}
runs outside the scope
* withTimeout(n)
* mutex.withLock {}
RxCoroutines -> Flow