Skip to content

Instantly share code, notes, and snippets.

@objcode
Created May 21, 2019 01:47
Show Gist options
  • Save objcode/7783d12fbd0ba0112c894145738c5245 to your computer and use it in GitHub Desktop.
Save objcode/7783d12fbd0ba0112c894145738c5245 to your computer and use it in GitHub Desktop.
Implement a one shot request with coroutines (Repository)
class ProductsRepository(val productsDao: ProductsDao) {
/**
* This is a "regular" suspending function, which means the caller must
* be in a coroutine. The repository is not responsible for starting or
* stopping coroutines since it doesn't have a natural lifecycle to cancel
* unnecessary work.
*
* This *may* be called from Dispatchers.Main and is main-safe because
* Room will take care of main-safety for us.
*/
suspend fun loadSortedProducts(ascending: Boolean): List<ProductListing> {
return if (ascending) {
productsDao.loadProductsByDateStockedAscending()
} else {
productsDao.loadProductsByDateStockedDescending()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment