This commit is contained in:
Benoit Marty 2019-11-05 11:05:29 +01:00
parent cd1a964067
commit 9d478dbfe2
2 changed files with 9 additions and 13 deletions

View File

@ -41,8 +41,7 @@ fun <T> doWithRealm(realmConfiguration: RealmConfiguration, action: (Realm) -> T
*/
fun <T : RealmObject> doRealmQueryAndCopy(realmConfiguration: RealmConfiguration, action: (Realm) -> T?): T? {
return Realm.getInstance(realmConfiguration).use { realm ->
val result = action.invoke(realm)
result?.let { realm.copyFromRealm(it) }
action.invoke(realm)?.let { realm.copyFromRealm(it) }
}
}
@ -51,8 +50,7 @@ fun <T : RealmObject> doRealmQueryAndCopy(realmConfiguration: RealmConfiguration
*/
fun <T : RealmObject> doRealmQueryAndCopyList(realmConfiguration: RealmConfiguration, action: (Realm) -> Iterable<T>): Iterable<T> {
return Realm.getInstance(realmConfiguration).use { realm ->
val result = action.invoke(realm)
realm.copyFromRealm(result)
action.invoke(realm).let { realm.copyFromRealm(it) }
}
}

View File

@ -91,7 +91,7 @@ internal class RealmCryptoStore(private val realmConfiguration: RealmConfigurati
realmLocker = Realm.getInstance(realmConfiguration)
// Ensure CryptoMetadataEntity is inserted in DB
doWithRealm(realmConfiguration) { realm ->
doRealmTransaction(realmConfiguration) { realm ->
var currentMetadata = realm.where<CryptoMetadataEntity>().findFirst()
var deleteAll = false
@ -109,15 +109,13 @@ internal class RealmCryptoStore(private val realmConfiguration: RealmConfigurati
}
if (currentMetadata == null) {
realm.executeTransaction {
if (deleteAll) {
it.deleteAll()
}
if (deleteAll) {
realm.deleteAll()
}
// Metadata not found, or database cleaned, create it
it.createObject(CryptoMetadataEntity::class.java, credentials.userId).apply {
deviceId = credentials.deviceId
}
// Metadata not found, or database cleaned, create it
realm.createObject(CryptoMetadataEntity::class.java, credentials.userId).apply {
deviceId = credentials.deviceId
}
}
}