mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2024-12-22 23:58:36 +01:00
Use a Set rather than an O(n) thing to unique DatabaseObjects.
This commit is contained in:
parent
666f1d9f4a
commit
b5d132717d
@ -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] {
|
||||||
|
|
||||||
// Can’t create a Set, because we can’t make a Set<DatabaseObject>, because protocol-conforming objects can’t 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 that’s 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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user