mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2024-12-28 10:30:14 +01:00
47 lines
1.1 KiB
Swift
47 lines
1.1 KiB
Swift
//
|
||
// RelatedObjectsMap.swift
|
||
// RSDatabase
|
||
//
|
||
// Created by Brent Simmons on 9/10/17.
|
||
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
||
//
|
||
|
||
import Foundation
|
||
|
||
// Map objectID to [DatabaseObject] (related objects).
|
||
// It’s used as the return value for DatabaseLookupTable.fetchRelatedObjects.
|
||
|
||
public struct RelatedObjectsMap {
|
||
|
||
private let dictionary: [String: [DatabaseObject]] // objectID: relatedObjects
|
||
|
||
init(relatedObjects: [DatabaseObject], relatedObjectIDsMap: RelatedObjectIDsMap) {
|
||
|
||
var d = [String: [DatabaseObject]]()
|
||
let relatedObjectsDictionary = relatedObjects.dictionary()
|
||
|
||
for objectID in relatedObjectIDsMap.objectIDs() {
|
||
|
||
if let relatedObjectIDs = relatedObjectIDsMap[objectID] {
|
||
let relatedObjects = relatedObjectIDs.flatMap{ relatedObjectsDictionary[$0] }
|
||
if !relatedObjects.isEmpty {
|
||
d[objectID] = relatedObjects
|
||
}
|
||
}
|
||
}
|
||
|
||
self.dictionary = d
|
||
}
|
||
|
||
public func objectIDs() -> Set<String> {
|
||
|
||
return Set(dictionary.keys)
|
||
}
|
||
|
||
public subscript(_ objectID: String) -> [DatabaseObject]? {
|
||
get {
|
||
return dictionary[objectID]
|
||
}
|
||
}
|
||
}
|