aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/db
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2019-03-05 19:13:57 -0500
committerAllan Wang <me@allanwang.ca>2019-03-05 19:13:57 -0500
commitb5d442ba3c86500b23d5bff8f1eb80ab51d1ccfa (patch)
tree28b296d8ce5d6217b55461aef850d96fa265fdf0 /app/src/main/kotlin/com/pitchedapps/frost/db
parent8841728780438444bf51f1a2c3b0d961e49908d2 (diff)
downloadfrost-b5d442ba3c86500b23d5bff8f1eb80ab51d1ccfa.tar.gz
frost-b5d442ba3c86500b23d5bff8f1eb80ab51d1ccfa.tar.bz2
frost-b5d442ba3c86500b23d5bff8f1eb80ab51d1ccfa.zip
Add more cookie db tests
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/db')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt31
1 files changed, 19 insertions, 12 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt
index ff64f5eb..44e62938 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/db/FbTabsDb.kt
@@ -33,6 +33,8 @@ import com.raizlabs.android.dbflow.kotlinextensions.fastSave
import com.raizlabs.android.dbflow.kotlinextensions.from
import com.raizlabs.android.dbflow.kotlinextensions.select
import com.raizlabs.android.dbflow.structure.BaseModel
+import kotlinx.coroutines.NonCancellable
+import kotlinx.coroutines.withContext
/**
* Created by Allan Wang on 2017-05-30.
@@ -52,21 +54,26 @@ interface FbTabDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun _insertAll(items: List<FbTabEntity>)
-
- @Transaction
- suspend fun _save(items: List<FbTabEntity>) {
- _deleteAll()
- _insertAll(items)
- }
}
+/**
+ * Saving tabs operates by deleting all db items and saving the new list.
+ * Transactions can't be done with suspensions in room as switching threads during the process
+ * may result in a deadlock.
+ * In this case, there may be a chance that the 'transaction' completes partially,
+ * but we'll just fallback to the default anyways.
+ */
suspend fun FbTabDao.save(items: List<FbItem>) {
- _save((items.takeIf { it.isNotEmpty() } ?: defaultTabs()).mapIndexed { index, fbItem ->
- FbTabEntity(
- index,
- fbItem
- )
- })
+ withContext(NonCancellable) {
+ _deleteAll()
+ val entities = (items.takeIf { it.isNotEmpty() } ?: defaultTabs()).mapIndexed { index, fbItem ->
+ FbTabEntity(
+ index,
+ fbItem
+ )
+ }
+ _insertAll(entities)
+ }
}
suspend fun FbTabDao.selectAll(): List<FbItem> = _selectAll().map { it.tab }.takeIf { it.isNotEmpty() } ?: defaultTabs()