Fix 'mutation during enumeration' and 'bad ordering' crashes.

pull/1/head
Matthew Chen 6 years ago
parent fc36fe4fcf
commit debf2e7a95

@ -49,8 +49,13 @@ public class OWS110SortIdMigration: OWSDatabaseMigration {
let totalCount: UInt = legacySorting.numberOfItemsInAllGroups() let totalCount: UInt = legacySorting.numberOfItemsInAllGroups()
var completedCount: UInt = 0 var completedCount: UInt = 0
var seenGroups: Set<String> = Set() var allGroups = [String]()
legacySorting.enumerateGroups { group, _ in legacySorting.enumerateGroups { group, _ in
allGroups.append(group)
}
var seenGroups: Set<String> = Set()
for group in allGroups {
autoreleasepool { autoreleasepool {
// Sanity Check #1 // Sanity Check #1
// Make sure our enumeration is monotonically increasing. // Make sure our enumeration is monotonically increasing.
@ -69,15 +74,20 @@ public class OWS110SortIdMigration: OWSDatabaseMigration {
} }
seenGroups.insert(group) 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 { autoreleasepool {
guard let interaction = object as? TSInteraction else { for uniqueId in batch {
owsFailDebug("unexpected object: \(type(of: object))") guard let interaction = TSInteraction.fetch(uniqueId: uniqueId, transaction: transaction) else {
owsFailDebug("Could not load interaction: \(uniqueId)")
return return
} }
if interaction.timestampForLegacySorting() < previousTimestampForLegacySorting {
guard interaction.timestampForLegacySorting() >= previousTimestampForLegacySorting else { owsFailDebug("unexpected object ordering previousTimestampForLegacySorting: \(previousTimestampForLegacySorting) interaction.timestampForLegacySorting: \(interaction.timestampForLegacySorting())")
owsFail("unexpected object ordering previousTimestampForLegacySorting: \(previousTimestampForLegacySorting) interaction.timestampForLegacySorting: \(interaction.timestampForLegacySorting)")
} }
previousTimestampForLegacySorting = interaction.timestampForLegacySorting() previousTimestampForLegacySorting = interaction.timestampForLegacySorting()
@ -92,6 +102,7 @@ public class OWS110SortIdMigration: OWSDatabaseMigration {
} }
} }
} }
}
Logger.info("re-archiving \(archivedThreads.count) threads which were previously archived") Logger.info("re-archiving \(archivedThreads.count) threads which were previously archived")
for archivedThread in archivedThreads { for archivedThread in archivedThreads {

Loading…
Cancel
Save