Created
May 21, 2019 01:47
-
-
Save objcode/7783d12fbd0ba0112c894145738c5245 to your computer and use it in GitHub Desktop.
Implement a one shot request with coroutines (Repository)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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