From 8c7af6f47b4d8f1f964e7515e9a31d0bc396b6da Mon Sep 17 00:00:00 2001 From: Gino Miceli <228050+gino-m@users.noreply.github.com> Date: Sat, 16 Nov 2024 22:39:10 -0500 Subject: [PATCH] Run survey update in transaction (#2835) --- .../local/room/stores/RoomSurveyStore.kt | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ground/src/main/java/com/google/android/ground/persistence/local/room/stores/RoomSurveyStore.kt b/ground/src/main/java/com/google/android/ground/persistence/local/room/stores/RoomSurveyStore.kt index 741a0d9361..2b4bbc4e90 100644 --- a/ground/src/main/java/com/google/android/ground/persistence/local/room/stores/RoomSurveyStore.kt +++ b/ground/src/main/java/com/google/android/ground/persistence/local/room/stores/RoomSurveyStore.kt @@ -15,12 +15,14 @@ */ package com.google.android.ground.persistence.local.room.stores +import androidx.room.withTransaction import com.google.android.ground.model.Survey import com.google.android.ground.model.job.Job import com.google.android.ground.model.task.Condition import com.google.android.ground.model.task.MultipleChoice import com.google.android.ground.model.task.Option import com.google.android.ground.model.task.Task +import com.google.android.ground.persistence.local.room.LocalDatabase import com.google.android.ground.persistence.local.room.converter.toLocalDataStoreObject import com.google.android.ground.persistence.local.room.converter.toModelObject import com.google.android.ground.persistence.local.room.dao.ConditionDao @@ -54,6 +56,8 @@ class RoomSurveyStore @Inject internal constructor() : LocalSurveyStore { @Inject lateinit var expressionDao: ExpressionDao + @Inject lateinit var localDatabase: LocalDatabase + override val surveys: Flow> get() = surveyDao.getAll().map { surveyEntities -> surveyEntities.map { it.toModelObject() } } @@ -63,11 +67,12 @@ class RoomSurveyStore @Inject internal constructor() : LocalSurveyStore { * Attempts to update persisted data associated with a [Survey] in the local database. If the * provided survey does not exist, inserts the given survey into the database. */ - override suspend fun insertOrUpdateSurvey(survey: Survey) { - surveyDao.insertOrUpdate(survey.toLocalDataStoreObject()) - jobDao.deleteBySurveyId(survey.id) - insertOrUpdateJobs(survey.id, survey.jobs) - } + override suspend fun insertOrUpdateSurvey(survey: Survey) = + localDatabase.withTransaction { + surveyDao.insertOrUpdate(survey.toLocalDataStoreObject()) + jobDao.deleteBySurveyId(survey.id) + insertOrUpdateJobs(survey.id, survey.jobs) + } /** * Returns the [Survey] with the given ID from the local database. Returns `null` if retrieval