123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- import Foundation
- import Realm
- public struct LinkingObjects<Element: Object> {
-
- 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 _: Element.Type, property propertyName: String) {
- self.propertyName = propertyName
- }
-
- public var description: String {
- if realm == nil {
- var this = self
- return withUnsafePointer(to: &this) {
- return "LinkingObjects<\(Element.className())> <\($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 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 func value(forKey key: String) -> Any? {
- return value(forKeyPath: key)
- }
-
- public func value(forKeyPath keyPath: String) -> Any? {
- return rlmResults.value(forKeyPath: keyPath)
- }
-
- public func setValue(_ value: Any?, forKey key: String) {
- return rlmResults.setValue(value, forKeyPath: key)
- }
-
-
- 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(on queue: DispatchQueue? = nil,
- _ block: @escaping (RealmCollectionChange<LinkingObjects>) -> Void) -> NotificationToken {
- return rlmResults.addNotificationBlock(wrapObserveBlock(block), queue: queue)
- }
-
-
- public var isFrozen: Bool { return self.rlmResults.isFrozen }
-
- public func freeze() -> LinkingObjects {
- return LinkingObjects(propertyName: propertyName, handle: handle?.freeze())
- }
-
- private init(propertyName: String, handle: RLMLinkingObjectsHandle?) {
- self.propertyName = propertyName
- self.handle = handle
- }
- internal init(objc: RLMResults<AnyObject>) {
- self.propertyName = ""
- self.handle = RLMLinkingObjectsHandle(linkingObjects: objc)
- }
- internal var rlmResults: RLMResults<AnyObject> {
- return handle?.results ?? RLMResults<AnyObject>.emptyDetached()
- }
- internal var propertyName: String
- internal var handle: RLMLinkingObjectsHandle?
- }
- extension LinkingObjects: RealmCollection {
-
-
- public func makeIterator() -> RLMIterator<Element> {
- return RLMIterator(collection: rlmResults)
- }
-
-
- public func _asNSFastEnumerator() -> Any {
- return 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(_ queue: DispatchQueue?,
- _ block: @escaping (RealmCollectionChange<AnyRealmCollection<Element>>) -> Void)
- -> NotificationToken {
- return rlmResults.addNotificationBlock(wrapObserveBlock(block), queue: queue)
- }
- }
- extension LinkingObjects: AssistedObjectiveCBridgeable {
- internal static func bridging(from objectiveCValue: Any, with metadata: Any?) -> LinkingObjects {
- guard let object = objectiveCValue as? RLMResults<Element> else { preconditionFailure() }
- return LinkingObjects<Element>(objc: object as! RLMResults<AnyObject>)
- }
- internal var bridged: (objectiveCValue: Any, metadata: Any?) {
- return (objectiveCValue: handle!.results, metadata: nil)
- }
- }
|