Skip to content

Instantly share code, notes, and snippets.

@recheej

recheej/Test.kt Secret

Last active July 21, 2020 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save recheej/387a0ea70552c0bff983b8028591f2cc to your computer and use it in GitHub Desktop.
Save recheej/387a0ea70552c0bff983b8028591f2cc to your computer and use it in GitHub Desktop.
Idiomatic way to perform non critical suspending work in a suspend function
class Repository(
private val database: Database,
private val nonCriticalScope: CoroutineScope,
private val networkRepository: NetworkRepository
) {
suspend storeItem(item: Item) {
nonCriticalScope.launch {
// this is a sync operation. I don't really care if it fails.
networkRepository.updateDataOnNetwork(item) //suspension point
}
database.insert(item) //suspension point
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment