123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- import Foundation
- import Realm
- public class LinkingObjectsBase: NSObject, NSFastEnumeration {
- internal let objectClassName: String
- internal let propertyName: String
- fileprivate var cachedRLMResults: RLMResults<AnyObject>?
- @objc fileprivate var object: RLMWeakObjectHandle?
- @objc fileprivate var property: RLMProperty?
- internal var rlmResults: RLMResults<AnyObject> {
- if cachedRLMResults == nil {
- if let object = self.object, let property = self.property {
- cachedRLMResults = RLMDynamicGet(object.object, property)! as? RLMResults
- self.object = nil
- self.property = nil
- } else {
- cachedRLMResults = RLMResults.emptyDetached()
- }
- }
- return cachedRLMResults!
- }
- init(fromClassName objectClassName: String, property propertyName: String) {
- self.objectClassName = objectClassName
- self.propertyName = propertyName
- }
-
- public func countByEnumerating(with state: UnsafeMutablePointer<NSFastEnumerationState>,
- objects buffer: AutoreleasingUnsafeMutablePointer<AnyObject?>,
- count len: Int) -> Int {
- return Int(rlmResults.countByEnumerating(with: state,
- objects: buffer,
- count: UInt(len)))
- }
- }
- public final class LinkingObjects<Element: Object>: LinkingObjectsBase {
-
- public typealias ElementType = Element
-
-
- public var realm: Realm? { return rlmResults.isAttached ? Realm(rlmResults.realm) : nil }
-
-
-
-
-
- public var isInvalidated: Bool { return rlmResults.isInvalidated }
-
- public var count: Int { return Int(rlmResults.count) }
-
-
- public init(fromType type: Element.Type, property propertyName: String) {
- let className = (Element.self as Object.Type).className()
- super.init(fromClassName: className, property: propertyName)
- }
-
- public override var description: String {
- if realm == nil {
- var this = self
- return withUnsafePointer(to: &this) {
- return "LinkingObjects<\(objectClassName)> <\($0)> (\n\n)"
- }
- }
- return RLMDescriptionWithMaxDepth("LinkingObjects", rlmResults, RLMDescriptionMaxDepth)
- }
-
-
- public func index(of object: Element) -> Int? {
- return notFoundToNil(index: rlmResults.index(of: object.unsafeCastToRLMObject()))
- }
-
- public func index(matching predicate: NSPredicate) -> Int? {
- return notFoundToNil(index: rlmResults.indexOfObject(with: predicate))
- }
-
- public func index(matching predicateFormat: String, _ args: Any...) -> Int? {
- return notFoundToNil(index: rlmResults.indexOfObject(with: NSPredicate(format: predicateFormat,
- argumentArray: unwrapOptionals(in: args))))
- }
-
-
- public subscript(index: Int) -> Element {
- throwForNegativeIndex(index)
- return unsafeBitCast(rlmResults[UInt(index)], to: Element.self)
- }
-
- public var first: Element? { return unsafeBitCast(rlmResults.firstObject(), to: Optional<Element>.self) }
-
- public var last: Element? { return unsafeBitCast(rlmResults.lastObject(), to: Optional<Element>.self) }
-
-
- public override func value(forKey key: String) -> Any? {
- return value(forKeyPath: key)
- }
-
- public override func value(forKeyPath keyPath: String) -> Any? {
- return rlmResults.value(forKeyPath: keyPath)
- }
-
- public override func setValue(_ value: Any?, forKey key: String) {
- return rlmResults.setValue(value, forKeyPath: key)
- }
-
-
- public func filter(_ predicateFormat: String, _ args: Any...) -> Results<Element> {
- return Results(rlmResults.objects(with: NSPredicate(format: predicateFormat,
- argumentArray: unwrapOptionals(in: args))))
- }
-
- public func filter(_ predicate: NSPredicate) -> Results<Element> {
- return Results(rlmResults.objects(with: predicate))
- }
-
-
- public func sorted(byKeyPath keyPath: String, ascending: Bool = true) -> Results<Element> {
- return sorted(by: [SortDescriptor(keyPath: keyPath, ascending: ascending)])
- }
-
- public func sorted<S: Sequence>(by sortDescriptors: S) -> Results<Element>
- where S.Iterator.Element == SortDescriptor {
- return Results(rlmResults.sortedResults(using: sortDescriptors.map { $0.rlmSortDescriptorValue }))
- }
-
-
- public func min<T: MinMaxType>(ofProperty property: String) -> T? {
- return rlmResults.min(ofProperty: property).map(dynamicBridgeCast)
- }
-
- public func max<T: MinMaxType>(ofProperty property: String) -> T? {
- return rlmResults.max(ofProperty: property).map(dynamicBridgeCast)
- }
-
- public func sum<T: AddableType>(ofProperty property: String) -> T {
- return dynamicBridgeCast(fromObjectiveC: rlmResults.sum(ofProperty: property))
- }
-
- public func average<T: AddableType>(ofProperty property: String) -> T? {
- return rlmResults.average(ofProperty: property).map(dynamicBridgeCast)
- }
-
-
- public func observe(_ block: @escaping (RealmCollectionChange<LinkingObjects>) -> Void) -> NotificationToken {
- return rlmResults.addNotificationBlock { _, change, error in
- block(RealmCollectionChange.fromObjc(value: self, change: change, error: error))
- }
- }
- }
- extension LinkingObjects: RealmCollection {
-
-
- public func makeIterator() -> RLMIterator<Element> {
- return RLMIterator(collection: rlmResults)
- }
-
-
-
- public var startIndex: Int { return 0 }
-
-
-
- public var endIndex: Int { return count }
- public func index(after: Int) -> Int {
- return after + 1
- }
- public func index(before: Int) -> Int {
- return before - 1
- }
-
- public func _observe(_ block: @escaping (RealmCollectionChange<AnyRealmCollection<Element>>) -> Void) ->
- NotificationToken {
- let anyCollection = AnyRealmCollection(self)
- return rlmResults.addNotificationBlock { _, change, error in
- block(RealmCollectionChange.fromObjc(value: anyCollection, change: change, error: error))
- }
- }
- }
- extension LinkingObjects: AssistedObjectiveCBridgeable {
- internal static func bridging(from objectiveCValue: Any, with metadata: Any?) -> LinkingObjects {
- guard let metadata = metadata as? LinkingObjectsBridgingMetadata else { preconditionFailure() }
- let swiftValue = LinkingObjects(fromType: Element.self, property: metadata.propertyName)
- switch (objectiveCValue, metadata) {
- case (let object as RLMObjectBase, .uncached(let property)):
- swiftValue.object = RLMWeakObjectHandle(object: object)
- swiftValue.property = property
- case (let results as RLMResults<AnyObject>, .cached):
- swiftValue.cachedRLMResults = results
- default:
- preconditionFailure()
- }
- return swiftValue
- }
- internal var bridged: (objectiveCValue: Any, metadata: Any?) {
- if let results = cachedRLMResults {
- return (objectiveCValue: results,
- metadata: LinkingObjectsBridgingMetadata.cached(propertyName: propertyName))
- } else {
- return (objectiveCValue: (object!.copy() as! RLMWeakObjectHandle).object,
- metadata: LinkingObjectsBridgingMetadata.uncached(property: property!))
- }
- }
- }
- internal enum LinkingObjectsBridgingMetadata {
- case uncached(property: RLMProperty)
- case cached(propertyName: String)
- fileprivate var propertyName: String {
- switch self {
- case .uncached(let property): return property.name
- case .cached(let propertyName): return propertyName
- }
- }
- }
|