diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt index 101bfb96..92e25923 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt @@ -140,9 +140,7 @@ class ClientsFragment : Fragment(), MainScope by MainScope.Supervisor() { val wasWorking = TrafficRecorder.isWorking(client.mac) client.obtainRecord().apply { blocked = !blocked - GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED) { - AppDatabase.instance.clientRecordDao.update(this@apply) - } + AppDatabase.instance.clientRecordDao.update(this) } IpNeighbourMonitor.instance?.flush() if (!wasWorking && item.itemId == R.id.block) { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TrafficRecorder.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TrafficRecorder.kt index 0c856f5b..3d61b028 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TrafficRecorder.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TrafficRecorder.kt @@ -10,10 +10,6 @@ import be.mygod.vpnhotspot.util.Event2 import be.mygod.vpnhotspot.util.RootSession import be.mygod.vpnhotspot.util.parseNumericAddress 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 java.net.InetAddress import java.util.concurrent.TimeUnit @@ -29,9 +25,7 @@ object TrafficRecorder { fun register(ip: InetAddress, downstream: String, mac: Long) { 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) { DebugHelper.log(TAG, "Registering $ip%$downstream") check(records.put(Pair(ip, downstream), record) == null) @@ -123,9 +117,7 @@ object TrafficRecorder { check(record.sentBytes >= 0) check(record.receivedPackets >= 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) } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/room/AppDatabase.kt b/mobile/src/main/java/be/mygod/vpnhotspot/room/AppDatabase.kt index f8ab1baf..d97dda58 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/room/AppDatabase.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/room/AppDatabase.kt @@ -19,6 +19,7 @@ abstract class AppDatabase : RoomDatabase() { .addMigrations( Migration2 ) + .allowMainThreadQueries() .build() } } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/room/ClientRecord.kt b/mobile/src/main/java/be/mygod/vpnhotspot/room/ClientRecord.kt index 7152ea27..d7159858 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/room/ClientRecord.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/room/ClientRecord.kt @@ -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 @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) } } } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/room/TrafficRecord.kt b/mobile/src/main/java/be/mygod/vpnhotspot/room/TrafficRecord.kt index 7cab74d7..7ebc224d 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/room/TrafficRecord.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/room/TrafficRecord.kt @@ -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) }