Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run DAO operation on Dispatchers.IO (end) #433

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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