Skip to content

Commit

Permalink
Run DAO operation on Dispatchers.IO (main) (#432)
Browse files Browse the repository at this point in the history
* Run DAO operation on Dispatchers.IO.

* Use withContext() to set dispatcher.

* Remove import.

* Fix test.
  • Loading branch information
arriolac authored Apr 4, 2024
1 parent ec22e81 commit 152ef0d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.google.samples.apps.sunflower.data.GardenPlantingRepository
import com.google.samples.apps.sunflower.data.PlantRepository
import com.google.samples.apps.sunflower.utilities.getValue
import com.google.samples.apps.sunflower.utilities.testPlant
import kotlinx.coroutines.Dispatchers
import org.junit.After
import org.junit.Assert.assertFalse
import org.junit.Before
Expand All @@ -45,7 +46,8 @@ class PlantDetailViewModelTest {

val plantRepo = PlantRepository.getInstance(appDatabase.plantDao())
val gardenPlantingRepo = GardenPlantingRepository.getInstance(
appDatabase.gardenPlantingDao()
appDatabase.gardenPlantingDao(),
Dispatchers.IO
)
viewModel = PlantDetailViewModel(plantRepo, gardenPlantingRepo, testPlant.plantId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@

package com.google.samples.apps.sunflower.data

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class GardenPlantingRepository private constructor(
private val gardenPlantingDao: GardenPlantingDao
private val gardenPlantingDao: GardenPlantingDao,
private val ioDispatcher: CoroutineDispatcher
) {

suspend fun createGardenPlanting(plantId: String) {
val gardenPlanting = GardenPlanting(plantId)
gardenPlantingDao.insertGardenPlanting(gardenPlanting)
withContext(ioDispatcher) {
val gardenPlanting = GardenPlanting(plantId)
gardenPlantingDao.insertGardenPlanting(gardenPlanting)
}
}

suspend fun removeGardenPlanting(gardenPlanting: GardenPlanting) {
Expand All @@ -39,9 +46,11 @@ class GardenPlantingRepository private constructor(
// For Singleton instantiation
@Volatile private var instance: GardenPlantingRepository? = null

fun getInstance(gardenPlantingDao: GardenPlantingDao) =
fun getInstance(gardenPlantingDao: GardenPlantingDao, ioDispatcher: CoroutineDispatcher) =
instance ?: synchronized(this) {
instance ?: GardenPlantingRepository(gardenPlantingDao).also { instance = it }
instance ?: GardenPlantingRepository(gardenPlantingDao, ioDispatcher).also {
instance = it
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ import com.google.samples.apps.sunflower.data.PlantRepository
import com.google.samples.apps.sunflower.viewmodels.GardenPlantingListViewModelFactory
import com.google.samples.apps.sunflower.viewmodels.PlantDetailViewModelFactory
import com.google.samples.apps.sunflower.viewmodels.PlantListViewModelFactory
import kotlinx.coroutines.Dispatchers

/**
* Static methods used to inject classes needed for various Activities and Fragments.
*/
object InjectorUtils {

private val ioDispatcher = Dispatchers.IO

private fun getPlantRepository(context: Context): PlantRepository {
return PlantRepository.getInstance(
AppDatabase.getInstance(context.applicationContext).plantDao()
Expand All @@ -38,7 +41,8 @@ object InjectorUtils {

private fun getGardenPlantingRepository(context: Context): GardenPlantingRepository {
return GardenPlantingRepository.getInstance(
AppDatabase.getInstance(context.applicationContext).gardenPlantingDao()
AppDatabase.getInstance(context.applicationContext).gardenPlantingDao(),
ioDispatcher
)
}

Expand Down

0 comments on commit 152ef0d

Please sign in to comment.