Merge pull request #667 from vector-im/feature/realm_cleanup

Import change form https://github.com/matrix-org/matrix-android-sdk/pull/505
This commit is contained in:
Benoit Marty 2019-11-05 12:13:25 +01:00 committed by GitHub
commit fb3e953e28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 14 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? { fun <T : RealmObject> doRealmQueryAndCopy(realmConfiguration: RealmConfiguration, action: (Realm) -> T?): T? {
return Realm.getInstance(realmConfiguration).use { realm -> return Realm.getInstance(realmConfiguration).use { realm ->
val result = action.invoke(realm) action.invoke(realm)?.let { realm.copyFromRealm(it) }
result?.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> { fun <T : RealmObject> doRealmQueryAndCopyList(realmConfiguration: RealmConfiguration, action: (Realm) -> Iterable<T>): Iterable<T> {
return Realm.getInstance(realmConfiguration).use { realm -> return Realm.getInstance(realmConfiguration).use { realm ->
val result = action.invoke(realm) action.invoke(realm).let { realm.copyFromRealm(it) }
realm.copyFromRealm(result)
} }
} }

View File

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

View File

@ -56,7 +56,6 @@ object CommandParser {
return ParsedCommand.ErrorEmptySlashCommand return ParsedCommand.ErrorEmptySlashCommand
} }
return when (val slashCommand = messageParts.first()) { return when (val slashCommand = messageParts.first()) {
Command.CHANGE_DISPLAY_NAME.command -> { Command.CHANGE_DISPLAY_NAME.command -> {
val newDisplayName = textMessage.substring(Command.CHANGE_DISPLAY_NAME.command.length).trim() val newDisplayName = textMessage.substring(Command.CHANGE_DISPLAY_NAME.command.length).trim()