Insert all recipes in one query
This commit is contained in:
@@ -23,23 +23,17 @@ class RecipeStorageImpl @Inject constructor(
|
|||||||
) : RecipeStorage {
|
) : RecipeStorage {
|
||||||
private val recipeDao: RecipeDao by lazy { db.recipeDao() }
|
private val recipeDao: RecipeDao by lazy { db.recipeDao() }
|
||||||
|
|
||||||
override suspend fun saveRecipes(
|
override suspend fun saveRecipes(recipes: List<RecipeSummaryInfo>) {
|
||||||
recipes: List<RecipeSummaryInfo>
|
|
||||||
) = db.withTransaction {
|
|
||||||
logger.v { "saveRecipes() called with $recipes" }
|
logger.v { "saveRecipes() called with $recipes" }
|
||||||
|
val entities = recipes.map { it.toRecipeSummaryEntity() }
|
||||||
for (recipe in recipes) {
|
logger.v { "saveRecipes: entities = $entities" }
|
||||||
val recipeSummaryEntity = recipe.toRecipeSummaryEntity()
|
db.withTransaction { recipeDao.insertRecipes(entities) }
|
||||||
recipeDao.insertRecipe(recipeSummaryEntity)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
override fun queryRecipes(query: String?): PagingSource<Int, RecipeSummaryEntity> {
|
override fun queryRecipes(query: String?): PagingSource<Int, RecipeSummaryEntity> {
|
||||||
logger.v { "queryRecipes() called with: query = $query" }
|
logger.v { "queryRecipes() called with: query = $query" }
|
||||||
return if (query == null) recipeDao.queryRecipesByPages()
|
return if (query == null) recipeDao.queryRecipesByPages()
|
||||||
else recipeDao.queryRecipesByPages(query)
|
else recipeDao.queryRecipesByPages(query)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun refreshAll(recipes: List<RecipeSummaryInfo>) {
|
override suspend fun refreshAll(recipes: List<RecipeSummaryInfo>) {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ interface RecipeDao {
|
|||||||
fun queryRecipesByPages(query: String): PagingSource<Int, RecipeSummaryEntity>
|
fun queryRecipesByPages(query: String): PagingSource<Int, RecipeSummaryEntity>
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
suspend fun insertRecipe(recipeSummaryEntity: RecipeSummaryEntity)
|
suspend fun insertRecipes(recipeSummaryEntity: Iterable<RecipeSummaryEntity>)
|
||||||
|
|
||||||
@Query("DELETE FROM recipe_summaries")
|
@Query("DELETE FROM recipe_summaries")
|
||||||
suspend fun removeAllRecipes()
|
suspend fun removeAllRecipes()
|
||||||
|
|||||||
Reference in New Issue
Block a user