Skip to content

Commit 152ef0d

Browse files
authored
Run DAO operation on Dispatchers.IO (main) (#432)
* Run DAO operation on Dispatchers.IO. * Use withContext() to set dispatcher. * Remove import. * Fix test.
1 parent ec22e81 commit 152ef0d

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

MigrationCodelab/app/src/androidTest/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModelTest.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.google.samples.apps.sunflower.data.GardenPlantingRepository
2424
import com.google.samples.apps.sunflower.data.PlantRepository
2525
import com.google.samples.apps.sunflower.utilities.getValue
2626
import com.google.samples.apps.sunflower.utilities.testPlant
27+
import kotlinx.coroutines.Dispatchers
2728
import org.junit.After
2829
import org.junit.Assert.assertFalse
2930
import org.junit.Before
@@ -45,7 +46,8 @@ class PlantDetailViewModelTest {
4546

4647
val plantRepo = PlantRepository.getInstance(appDatabase.plantDao())
4748
val gardenPlantingRepo = GardenPlantingRepository.getInstance(
48-
appDatabase.gardenPlantingDao()
49+
appDatabase.gardenPlantingDao(),
50+
Dispatchers.IO
4951
)
5052
viewModel = PlantDetailViewModel(plantRepo, gardenPlantingRepo, testPlant.plantId)
5153
}

MigrationCodelab/app/src/main/java/com/google/samples/apps/sunflower/data/GardenPlantingRepository.kt

+14-5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@
1616

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

19+
import kotlinx.coroutines.CoroutineDispatcher
20+
import kotlinx.coroutines.Dispatchers
21+
import kotlinx.coroutines.withContext
22+
1923
class GardenPlantingRepository private constructor(
20-
private val gardenPlantingDao: GardenPlantingDao
24+
private val gardenPlantingDao: GardenPlantingDao,
25+
private val ioDispatcher: CoroutineDispatcher
2126
) {
2227

2328
suspend fun createGardenPlanting(plantId: String) {
24-
val gardenPlanting = GardenPlanting(plantId)
25-
gardenPlantingDao.insertGardenPlanting(gardenPlanting)
29+
withContext(ioDispatcher) {
30+
val gardenPlanting = GardenPlanting(plantId)
31+
gardenPlantingDao.insertGardenPlanting(gardenPlanting)
32+
}
2633
}
2734

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

42-
fun getInstance(gardenPlantingDao: GardenPlantingDao) =
49+
fun getInstance(gardenPlantingDao: GardenPlantingDao, ioDispatcher: CoroutineDispatcher) =
4350
instance ?: synchronized(this) {
44-
instance ?: GardenPlantingRepository(gardenPlantingDao).also { instance = it }
51+
instance ?: GardenPlantingRepository(gardenPlantingDao, ioDispatcher).also {
52+
instance = it
53+
}
4554
}
4655
}
4756
}

MigrationCodelab/app/src/main/java/com/google/samples/apps/sunflower/utilities/InjectorUtils.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ import com.google.samples.apps.sunflower.data.PlantRepository
2424
import com.google.samples.apps.sunflower.viewmodels.GardenPlantingListViewModelFactory
2525
import com.google.samples.apps.sunflower.viewmodels.PlantDetailViewModelFactory
2626
import com.google.samples.apps.sunflower.viewmodels.PlantListViewModelFactory
27+
import kotlinx.coroutines.Dispatchers
2728

2829
/**
2930
* Static methods used to inject classes needed for various Activities and Fragments.
3031
*/
3132
object InjectorUtils {
3233

34+
private val ioDispatcher = Dispatchers.IO
35+
3336
private fun getPlantRepository(context: Context): PlantRepository {
3437
return PlantRepository.getInstance(
3538
AppDatabase.getInstance(context.applicationContext).plantDao()
@@ -38,7 +41,8 @@ object InjectorUtils {
3841

3942
private fun getGardenPlantingRepository(context: Context): GardenPlantingRepository {
4043
return GardenPlantingRepository.getInstance(
41-
AppDatabase.getInstance(context.applicationContext).gardenPlantingDao()
44+
AppDatabase.getInstance(context.applicationContext).gardenPlantingDao(),
45+
ioDispatcher
4246
)
4347
}
4448

0 commit comments

Comments
 (0)