diff --git a/SignalMessaging/environment/migrations/OWS110SortIdMigration.swift b/SignalMessaging/environment/migrations/OWS110SortIdMigration.swift index 16836267a..3d9df150c 100644 --- a/SignalMessaging/environment/migrations/OWS110SortIdMigration.swift +++ b/SignalMessaging/environment/migrations/OWS110SortIdMigration.swift @@ -49,8 +49,13 @@ public class OWS110SortIdMigration: OWSDatabaseMigration { let totalCount: UInt = legacySorting.numberOfItemsInAllGroups() var completedCount: UInt = 0 - var seenGroups: Set = Set() + var allGroups = [String]() legacySorting.enumerateGroups { group, _ in + allGroups.append(group) + } + + var seenGroups: Set = Set() + for group in allGroups { autoreleasepool { // Sanity Check #1 // Make sure our enumeration is monotonically increasing. @@ -69,24 +74,30 @@ public class OWS110SortIdMigration: OWSDatabaseMigration { } seenGroups.insert(group) - legacySorting.enumerateKeysAndObjects(inGroup: group) { (_, _, object, _, _) in + var groupKeys = [String]() + legacySorting.enumerateKeys(inGroup: group, using: { (_, key, _, _) in + groupKeys.append(key) + }) + let groupKeyBatchSize: Int = 1024 + for batch in groupKeys.chunked(by: groupKeyBatchSize) { autoreleasepool { - guard let interaction = object as? TSInteraction else { - owsFailDebug("unexpected object: \(type(of: object))") - return - } - - guard interaction.timestampForLegacySorting() >= previousTimestampForLegacySorting else { - owsFail("unexpected object ordering previousTimestampForLegacySorting: \(previousTimestampForLegacySorting) interaction.timestampForLegacySorting: \(interaction.timestampForLegacySorting)") - } - previousTimestampForLegacySorting = interaction.timestampForLegacySorting() - - interaction.saveNextSortId(transaction: transaction) - - completedCount += 1 - if completedCount % 100 == 0 { - // Legit usage of legacy sorting for migration to new sorting - Logger.info("thread: \(interaction.uniqueThreadId), timestampForLegacySorting:\(interaction.timestampForLegacySorting()), sortId: \(interaction.sortId) totalCount: \(totalCount), completedcount: \(completedCount)") + for uniqueId in batch { + guard let interaction = TSInteraction.fetch(uniqueId: uniqueId, transaction: transaction) else { + owsFailDebug("Could not load interaction: \(uniqueId)") + return + } + if interaction.timestampForLegacySorting() < previousTimestampForLegacySorting { + owsFailDebug("unexpected object ordering previousTimestampForLegacySorting: \(previousTimestampForLegacySorting) interaction.timestampForLegacySorting: \(interaction.timestampForLegacySorting())") + } + previousTimestampForLegacySorting = interaction.timestampForLegacySorting() + + interaction.saveNextSortId(transaction: transaction) + + completedCount += 1 + if completedCount % 100 == 0 { + // Legit usage of legacy sorting for migration to new sorting + Logger.info("thread: \(interaction.uniqueThreadId), timestampForLegacySorting:\(interaction.timestampForLegacySorting()), sortId: \(interaction.sortId) totalCount: \(totalCount), completedcount: \(completedCount)") + } } } }