Crypto store: fix potential issue with realm open/close process

This commit is contained in:
ganfra 2019-10-04 19:36:10 +02:00
parent 0582d0f641
commit 4e4fb4c565
2 changed files with 16 additions and 19 deletions

View file

@ -32,41 +32,38 @@ import java.util.zip.GZIPInputStream
* Get realm, invoke the action, close realm, and return the result of the action
*/
fun <T> doWithRealm(realmConfiguration: RealmConfiguration, action: (Realm) -> T): T {
val realm = Realm.getInstance(realmConfiguration)
val result = action.invoke(realm)
realm.close()
return result
return Realm.getInstance(realmConfiguration).use { realm ->
action.invoke(realm)
}
}
/**
* Get realm, do the query, copy from realm, close realm, and return the copied result
*/
fun <T : RealmObject> doRealmQueryAndCopy(realmConfiguration: RealmConfiguration, action: (Realm) -> T?): T? {
val realm = Realm.getInstance(realmConfiguration)
return Realm.getInstance(realmConfiguration).use { realm ->
val result = action.invoke(realm)
val copiedResult = result?.let { realm.copyFromRealm(result) }
realm.close()
return copiedResult
result?.let { realm.copyFromRealm(result) }
}
}
/**
* Get realm, do the list query, copy from realm, close realm, and return the copied result
*/
fun <T : RealmObject> doRealmQueryAndCopyList(realmConfiguration: RealmConfiguration, action: (Realm) -> Iterable<T>): Iterable<T> {
val realm = Realm.getInstance(realmConfiguration)
return Realm.getInstance(realmConfiguration).use { realm ->
val result = action.invoke(realm)
val copiedResult = realm.copyFromRealm(result)
realm.close()
return copiedResult
return realm.copyFromRealm(result)
}
}
/**
* Get realm instance, invoke the action in a transaction and close realm
*/
fun doRealmTransaction(realmConfiguration: RealmConfiguration, action: (Realm) -> Unit) {
val realm = Realm.getInstance(realmConfiguration)
realm.executeTransaction { action.invoke(it) }
realm.close()
Realm.getInstance(realmConfiguration).use { realm ->
realm.executeTransaction { action.invoke(realm) }
}
}
/**

View file

@ -360,7 +360,7 @@ internal class RealmCryptoStore(private val realmConfiguration: RealmConfigurati
return
}
doRealmTransaction(realmConfiguration) {
doRealmTransaction(realmConfiguration) { realm ->
sessions.forEach { session ->
var sessionIdentifier: String? = null
@ -387,7 +387,7 @@ internal class RealmCryptoStore(private val realmConfiguration: RealmConfigurati
putInboundGroupSession(session)
}
it.insertOrUpdate(realmOlmInboundGroupSession)
realm.insertOrUpdate(realmOlmInboundGroupSession)
}
}
}