From c9a28c1cf1c0202d28967655d056b4d0beb77fbc Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 27 Jun 2022 14:49:34 +0200 Subject: [PATCH] Fix migration issue. We had an infinite migration, since `clearWith` wait for the list to be empty. --- .../database/migration/MigrateSessionTo030.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo030.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo030.kt index f9c4a8b642..21348d5b8b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo030.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo030.kt @@ -17,11 +17,12 @@ package org.matrix.android.sdk.internal.database.migration import io.realm.DynamicRealm +import io.realm.DynamicRealmObject import org.matrix.android.sdk.internal.database.model.ChunkEntityFields import org.matrix.android.sdk.internal.database.model.EventEntityFields import org.matrix.android.sdk.internal.database.model.TimelineEventEntityFields -import org.matrix.android.sdk.internal.extensions.clearWith import org.matrix.android.sdk.internal.util.database.RealmMigrator +import timber.log.Timber /** * Migrating to: @@ -35,16 +36,23 @@ internal class MigrateSessionTo030(realm: DynamicRealm) : RealmMigrator(realm, 3 .equalTo(ChunkEntityFields.IS_LAST_FORWARD, false) .findAll() + var nbOfDeletedObjects = 0 chunks.forEach { chunk -> - chunk.getList(ChunkEntityFields.TIMELINE_EVENTS.`$`).clearWith { timelineEvent -> + val realmObjectToDelete = mutableListOf() + chunk.getList(ChunkEntityFields.TIMELINE_EVENTS.`$`).forEach { timelineEvent -> // Don't delete state events val event = timelineEvent.getObject(TimelineEventEntityFields.ROOT.`$`) if (event?.isNull(EventEntityFields.STATE_KEY) == true) { - event.deleteFromRealm() - timelineEvent.deleteFromRealm() + realmObjectToDelete.add(event) + realmObjectToDelete.add(timelineEvent) } } + nbOfDeletedObjects += realmObjectToDelete.size + realmObjectToDelete.forEach { + it.deleteFromRealm() + } chunk.deleteFromRealm() } + Timber.d("MigrateSessionTo030: $nbOfDeletedObjects deleted object(s).") } }