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

@@ -140,9 +140,7 @@ class ClientsFragment : Fragment(), MainScope by MainScope.Supervisor() {
val wasWorking = TrafficRecorder.isWorking(client.mac) val wasWorking = TrafficRecorder.isWorking(client.mac)
client.obtainRecord().apply { client.obtainRecord().apply {
blocked = !blocked blocked = !blocked
GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED) { AppDatabase.instance.clientRecordDao.update(this)
AppDatabase.instance.clientRecordDao.update(this@apply)
}
} }
IpNeighbourMonitor.instance?.flush() IpNeighbourMonitor.instance?.flush()
if (!wasWorking && item.itemId == R.id.block) { if (!wasWorking && item.itemId == R.id.block) {

View File

@@ -10,10 +10,6 @@ import be.mygod.vpnhotspot.util.Event2
import be.mygod.vpnhotspot.util.RootSession import be.mygod.vpnhotspot.util.RootSession
import be.mygod.vpnhotspot.util.parseNumericAddress import be.mygod.vpnhotspot.util.parseNumericAddress
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import java.net.InetAddress import java.net.InetAddress
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@@ -29,9 +25,7 @@ object TrafficRecorder {
fun register(ip: InetAddress, downstream: String, mac: Long) { fun register(ip: InetAddress, downstream: String, mac: Long) {
val record = TrafficRecord(mac = mac, ip = ip, downstream = downstream) val record = TrafficRecord(mac = mac, ip = ip, downstream = downstream)
GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED) { AppDatabase.instance.trafficRecordDao.insert(record)
AppDatabase.instance.trafficRecordDao.insert(record)
}
synchronized(this) { synchronized(this) {
DebugHelper.log(TAG, "Registering $ip%$downstream") DebugHelper.log(TAG, "Registering $ip%$downstream")
check(records.put(Pair(ip, downstream), record) == null) check(records.put(Pair(ip, downstream), record) == null)
@@ -123,9 +117,7 @@ object TrafficRecorder {
check(record.sentBytes >= 0) check(record.sentBytes >= 0)
check(record.receivedPackets >= 0) check(record.receivedPackets >= 0)
check(record.receivedBytes >= 0) check(record.receivedBytes >= 0)
GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED) { AppDatabase.instance.trafficRecordDao.insert(record)
AppDatabase.instance.trafficRecordDao.insert(record)
}
} }
foregroundListeners(records.values, oldRecords) foregroundListeners(records.values, oldRecords)
} }

View File

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

View File

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