mirror of
https://github.com/element-hq/element-android
synced 2024-11-24 02:15:35 +03:00
Allow returning a value from an async transaction
This commit is contained in:
parent
fb8ba32fb4
commit
aa9d66b991
3 changed files with 8 additions and 6 deletions
|
@ -22,12 +22,13 @@ import kotlinx.coroutines.isActive
|
|||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
|
||||
suspend fun awaitTransaction(config: RealmConfiguration, transaction: suspend (realm: Realm) -> Unit) = withContext(Dispatchers.Default) {
|
||||
suspend fun <T> awaitTransaction(config: RealmConfiguration, transaction: suspend (realm: Realm) -> T) = withContext(Dispatchers.Default) {
|
||||
Realm.getInstance(config).use { bgRealm ->
|
||||
bgRealm.beginTransaction()
|
||||
val result: T
|
||||
try {
|
||||
val start = System.currentTimeMillis()
|
||||
transaction(bgRealm)
|
||||
result = transaction(bgRealm)
|
||||
if (isActive) {
|
||||
bgRealm.commitTransaction()
|
||||
val end = System.currentTimeMillis()
|
||||
|
@ -39,5 +40,6 @@ suspend fun awaitTransaction(config: RealmConfiguration, transaction: suspend (r
|
|||
bgRealm.cancelTransaction()
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ internal class DefaultFilterRepository @Inject constructor(private val monarchy:
|
|||
}
|
||||
|
||||
override suspend fun getFilter(): String {
|
||||
return Realm.getInstance(monarchy.realmConfiguration).use {
|
||||
return monarchy.awaitTransaction {
|
||||
val filter = FilterEntity.getOrCreate(it)
|
||||
if (filter.filterId.isBlank()) {
|
||||
// Use the Json format
|
||||
|
@ -82,7 +82,7 @@ internal class DefaultFilterRepository @Inject constructor(private val monarchy:
|
|||
}
|
||||
|
||||
override suspend fun getRoomFilter(): String {
|
||||
return Realm.getInstance(monarchy.realmConfiguration).use {
|
||||
return monarchy.awaitTransaction {
|
||||
FilterEntity.getOrCreate(it).roomEventFilterJson
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,8 +22,8 @@ import io.realm.Realm
|
|||
import io.realm.RealmModel
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
|
||||
internal suspend fun Monarchy.awaitTransaction(transaction: suspend (realm: Realm) -> Unit) {
|
||||
awaitTransaction(realmConfiguration, transaction)
|
||||
internal suspend fun <T> Monarchy.awaitTransaction(transaction: suspend (realm: Realm) -> T): T {
|
||||
return awaitTransaction(realmConfiguration, transaction)
|
||||
}
|
||||
|
||||
fun <T : RealmModel> Monarchy.fetchCopied(query: (Realm) -> T?): T? {
|
||||
|
|
Loading…
Reference in a new issue