Use a Set rather than an O(n) thing to unique DatabaseObjects.

This commit is contained in:
Brent Simmons 2024-05-26 12:13:01 -07:00
parent 666f1d9f4a
commit b5d132717d

View File

@ -126,7 +126,7 @@ private extension DatabaseLookupTable {
// Save the actual related objects. // Save the actual related objects.
let relatedObjectsToSave = uniqueArrayOfRelatedObjects(with: objects) let relatedObjectsToSave = uniqueRelatedObjects(with: objects)
if relatedObjectsToSave.isEmpty { if relatedObjectsToSave.isEmpty {
assertionFailure("updateRelationships: expected relatedObjectsToSave would not be empty. This should be unreachable.") assertionFailure("updateRelationships: expected relatedObjectsToSave would not be empty. This should be unreachable.")
return return
@ -135,24 +135,12 @@ private extension DatabaseLookupTable {
relatedTable.save(relatedObjectsToSave, in: database) relatedTable.save(relatedObjectsToSave, in: database)
} }
func uniqueArrayOfRelatedObjects(with objects: [DatabaseObject]) -> [DatabaseObject] { func uniqueRelatedObjects(with objects: [DatabaseObject]) -> [DatabaseObject] {
// Cant create a Set, because we cant make a Set<DatabaseObject>, because protocol-conforming objects cant be made Hashable or even Equatable.
// We still want the array to include only one copy of each object, but we have to do it the slow way. Instruments will tell us if this is a performance problem.
var relatedObjectsUniqueArray = [DatabaseObject]() // All of our objects that conform to DatabaseObject are Hashable.
for object in objects { // If thats ever not true, this will crash when run from Xcode.
guard let relatedObjects = object.relatedObjectsWithName(relationshipName) else { let objectSet = Set(objects as! [AnyHashable])
assertionFailure("uniqueArrayOfRelatedObjects: expected every object to have related objects.") return Array(objectSet) as! [DatabaseObject]
continue
}
for relatedObject in relatedObjects {
if !relatedObjectsUniqueArray.includesObjectWithDatabaseID(relatedObject.databaseID) {
relatedObjectsUniqueArray += [relatedObject]
}
}
}
return relatedObjectsUniqueArray
} }
func syncRelatedObjectsAndLookupTable(_ object: DatabaseObject, _ lookupTable: RelatedObjectIDsMap, _ database: FMDatabase) { func syncRelatedObjectsAndLookupTable(_ object: DatabaseObject, _ lookupTable: RelatedObjectIDsMap, _ database: FMDatabase) {