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 * Get realm, invoke the action, close realm, and return the result of the action
*/ */
fun <T> doWithRealm(realmConfiguration: RealmConfiguration, action: (Realm) -> T): T { fun <T> doWithRealm(realmConfiguration: RealmConfiguration, action: (Realm) -> T): T {
val realm = Realm.getInstance(realmConfiguration) return Realm.getInstance(realmConfiguration).use { realm ->
val result = action.invoke(realm) action.invoke(realm)
realm.close() }
return result
} }
/** /**
* Get realm, do the query, copy from realm, close realm, and return the copied result * 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? { 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 result = action.invoke(realm)
val copiedResult = result?.let { realm.copyFromRealm(result) } result?.let { realm.copyFromRealm(result) }
realm.close() }
return copiedResult
} }
/** /**
* Get realm, do the list query, copy from realm, close realm, and return the copied 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> { 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 result = action.invoke(realm)
val copiedResult = realm.copyFromRealm(result) return realm.copyFromRealm(result)
realm.close() }
return copiedResult
} }
/** /**
* Get realm instance, invoke the action in a transaction and close realm * Get realm instance, invoke the action in a transaction and close realm
*/ */
fun doRealmTransaction(realmConfiguration: RealmConfiguration, action: (Realm) -> Unit) { fun doRealmTransaction(realmConfiguration: RealmConfiguration, action: (Realm) -> Unit) {
val realm = Realm.getInstance(realmConfiguration) Realm.getInstance(realmConfiguration).use { realm ->
realm.executeTransaction { action.invoke(it) } realm.executeTransaction { action.invoke(realm) }
realm.close() }
} }
/** /**

View file

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