diff --git a/Frameworks/RSDatabase/DatabaseTable.swift b/Frameworks/RSDatabase/DatabaseTable.swift index bdcd9af0c..7c34eb536 100644 --- a/Frameworks/RSDatabase/DatabaseTable.swift +++ b/Frameworks/RSDatabase/DatabaseTable.swift @@ -14,6 +14,8 @@ public protocol DatabaseTable { var queue: RSDatabaseQueue {get} init(name: String, queue: RSDatabaseQueue) + + func fetchObjectsWithIDs(_ databaseIDs: Set, _ database: FMDatabase) -> [T] } public extension DatabaseTable { diff --git a/Frameworks/RSDatabase/RSDatabase/LookupTable.swift b/Frameworks/RSDatabase/RSDatabase/LookupTable.swift index 5ec2d604e..0b4248ef0 100644 --- a/Frameworks/RSDatabase/RSDatabase/LookupTable.swift +++ b/Frameworks/RSDatabase/RSDatabase/LookupTable.swift @@ -15,7 +15,7 @@ import Foundation // foreignIDsWithNoRelationship: caches the foreignIDs where it’s known that there’s no relationship. // lookupsByForeignID: caches the LookupValues for a foreignID. -typealias LookupTableDictionary = [String: Set] // key is foreignID +public typealias LookupTableDictionary = [String: Set] // key is foreignID public final class LookupTable { @@ -64,20 +64,30 @@ public final class LookupTable { return lookupTableDictionary(with: lookupValues) } - public func attachRelationships(_ objects: [T], idKeyPath: KeyPath, relationshipKeyPath: KeyPath, table: DatabaseTable, lookupTableDictionary: LookupTableDictionary, database: FMDatabase) { + public func attachRelationships(to objects: [T], idKeyPath: KeyPath, relatedIDKeyPath: KeyPath, relationshipKeyPath: KeyPath, table: DatabaseTable, lookupTableDictionary: LookupTableDictionary, database: FMDatabase) { let primaryIDs = primaryIDsInLookupTableDictionary(lookupTableDictionary) - let relatedObjects = table.fetchObjectsWithIDs(primaryIDs, database) + if (primaryIDs.isEmpty) { + return + } + + let relatedObjects: [U] = table.fetchObjectsWithIDs(primaryIDs, database) + if relatedObjects.isEmpty { + return + } + + let relatedObjectsDictionary = createRelatedObjectsDictionary(relatedObjects, idKeyPath: relatedIDKeyPath) for object in objects { let identifier = object[keyPath: idKeyPath] - if let lookupValues = lookupTableDictionary[identifier] { - let relatedObjects = lookupValues.flatMap{ (lookupValue) -> U? in - <#code#> + if let lookupValues = lookupTableDictionary[identifier], !lookupValues.isEmpty { + let primaryIDs = lookupValues.primaryIDs() + let oneObjectRelatedObjects = primaryIDs.flatMap{ (primaryID) -> U? in + return relatedObjectsDictionary[primaryID] } + object[keyPath: relationshipKeyPath] = oneObjectRelatedObjects } } - } func primaryIDsInLookupTableDictionary(_ lookupTableDictionary: LookupTableDictionary) -> Set { @@ -109,6 +119,18 @@ public final class LookupTable { private extension LookupTable { + func createRelatedObjectsDictionary(_ relatedObjects: [T], idKeyPath: KeyPath) -> [String: T] { + + var d = [String: T]() + + for object in relatedObjects { + let identifier = object[keyPath: idKeyPath] + d[identifier] = object + } + + return d + } + func addToLookupTableDictionary(_ lookupValues: Set, _ table: inout LookupTableDictionary) { for lookupValue in lookupValues {