Revert "No more main thread SQL"

This reverts commit 4de891b459.

Async writes are a bad idea.
This commit is contained in:
Mygod
2019-02-02 02:48:08 +08:00
parent bf697dafb1
commit 194ef92de3
5 changed files with 11 additions and 27 deletions

View File

@@ -19,6 +19,7 @@ abstract class AppDatabase : RoomDatabase() {
.addMigrations(
Migration2
)
.allowMainThreadQueries()
.build()
}
}

View File

@@ -2,9 +2,6 @@ package be.mygod.vpnhotspot.room
import androidx.lifecycle.LiveData
import androidx.room.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
@Entity
@@ -24,17 +21,13 @@ data class ClientRecord(@PrimaryKey
abstract fun lookupSync(mac: Long): LiveData<ClientRecord>
@Insert(onConflict = OnConflictStrategy.REPLACE)
protected abstract suspend fun updateInternal(value: ClientRecord): Long
suspend fun update(value: ClientRecord) = check(updateInternal(value) == value.mac)
protected abstract fun updateInternal(value: ClientRecord): Long
fun update(value: ClientRecord) = check(updateInternal(value) == value.mac)
@Transaction
protected open fun upsertSync(mac: Long, operation: ClientRecord.() -> Unit) = runBlocking {
lookupOrDefault(mac).apply {
operation()
update(this)
}
open fun upsert(mac: Long, operation: ClientRecord.() -> Unit) = runBlocking { lookupOrDefault(mac) }.apply {
operation()
update(this)
}
fun upsert(mac: Long, operation: ClientRecord.() -> Unit) =
GlobalScope.async(Dispatchers.IO) { upsertSync(mac, operation) }
}
}

View File

@@ -41,8 +41,8 @@ data class TrafficRecord(
@androidx.room.Dao
abstract class Dao {
@Insert
protected abstract suspend fun insertInternal(value: TrafficRecord): Long
suspend fun insert(value: TrafficRecord) {
protected abstract fun insertInternal(value: TrafficRecord): Long
fun insert(value: TrafficRecord) {
check(value.id == null)
value.id = insertInternal(value)
}