1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036 |
- import Foundation
- import Realm
- public struct RLMIterator<Element: RealmCollectionValue>: IteratorProtocol {
- private var generatorBase: NSFastEnumerationIterator
- init(collection: RLMCollection) {
- generatorBase = NSFastEnumerationIterator(collection)
- }
-
- public mutating func next() -> Element? {
- let next = generatorBase.next()
- if next is NSNull {
- return Element._nilValue()
- }
- if let next = next as? Object? {
- if next == nil {
- return nil as Element?
- }
- return unsafeBitCast(next, to: Optional<Element>.self)
- }
- return dynamicBridgeCast(fromObjectiveC: next as Any)
- }
- }
- public enum RealmCollectionChange<CollectionType> {
-
- case initial(CollectionType)
-
- case update(CollectionType, deletions: [Int], insertions: [Int], modifications: [Int])
-
- case error(Error)
- static func fromObjc(value: CollectionType, change: RLMCollectionChange?, error: Error?) -> RealmCollectionChange {
- if let error = error {
- return .error(error)
- }
- if let change = change {
- return .update(value,
- deletions: forceCast(change.deletions, to: [Int].self),
- insertions: forceCast(change.insertions, to: [Int].self),
- modifications: forceCast(change.modifications, to: [Int].self))
- }
- return .initial(value)
- }
- }
- private func forceCast<A, U>(_ from: A, to type: U.Type) -> U {
- return from as! U
- }
- public protocol RealmCollectionValue: Equatable {
-
- static func _rlmArray() -> RLMArray<AnyObject>
-
- static func _nilValue() -> Self
- }
- extension RealmCollectionValue {
-
- public static func _rlmArray() -> RLMArray<AnyObject> {
- return RLMArray(objectType: .int, optional: false)
- }
-
- public static func _nilValue() -> Self {
- fatalError("unexpected NSNull for non-Optional type")
- }
- }
- private func arrayType<T>(_ type: T.Type) -> RLMArray<AnyObject> {
- switch type {
- case is Int.Type, is Int8.Type, is Int16.Type, is Int32.Type, is Int64.Type:
- return RLMArray(objectType: .int, optional: true)
- case is Bool.Type: return RLMArray(objectType: .bool, optional: true)
- case is Float.Type: return RLMArray(objectType: .float, optional: true)
- case is Double.Type: return RLMArray(objectType: .double, optional: true)
- case is String.Type: return RLMArray(objectType: .string, optional: true)
- case is Data.Type: return RLMArray(objectType: .data, optional: true)
- case is Date.Type: return RLMArray(objectType: .date, optional: true)
- default: fatalError("Unsupported type for List: \(type)?")
- }
- }
- extension Optional: RealmCollectionValue where Wrapped: RealmCollectionValue {
-
- public static func _rlmArray() -> RLMArray<AnyObject> {
- return arrayType(Wrapped.self)
- }
-
- public static func _nilValue() -> Optional {
- return nil
- }
- }
- extension Int: RealmCollectionValue {}
- extension Int8: RealmCollectionValue {}
- extension Int16: RealmCollectionValue {}
- extension Int32: RealmCollectionValue {}
- extension Int64: RealmCollectionValue {}
- extension Float: RealmCollectionValue {
-
- public static func _rlmArray() -> RLMArray<AnyObject> {
- return RLMArray(objectType: .float, optional: false)
- }
- }
- extension Double: RealmCollectionValue {
-
- public static func _rlmArray() -> RLMArray<AnyObject> {
- return RLMArray(objectType: .double, optional: false)
- }
- }
- extension Bool: RealmCollectionValue {
-
- public static func _rlmArray() -> RLMArray<AnyObject> {
- return RLMArray(objectType: .bool, optional: false)
- }
- }
- extension String: RealmCollectionValue {
-
- public static func _rlmArray() -> RLMArray<AnyObject> {
- return RLMArray(objectType: .string, optional: false)
- }
- }
- extension Date: RealmCollectionValue {
-
- public static func _rlmArray() -> RLMArray<AnyObject> {
- return RLMArray(objectType: .date, optional: false)
- }
- }
- extension Data: RealmCollectionValue {
-
- public static func _rlmArray() -> RLMArray<AnyObject> {
- return RLMArray(objectType: .data, optional: false)
- }
- }
- public protocol _RealmCollectionEnumerator {
-
- func _asNSFastEnumerator() -> Any
- }
- public protocol RealmCollectionBase: RandomAccessCollection, LazyCollectionProtocol, CustomStringConvertible, ThreadConfined where Element: RealmCollectionValue {
-
-
- typealias ElementType = Element
- }
- public protocol RealmCollection: RealmCollectionBase, _RealmCollectionEnumerator {
-
-
-
- var realm: Realm? { get }
-
- var isInvalidated: Bool { get }
-
- var count: Int { get }
-
- var description: String { get }
-
-
- func index(of object: Element) -> Int?
-
- func index(matching predicate: NSPredicate) -> Int?
-
- func index(matching predicateFormat: String, _ args: Any...) -> Int?
-
-
- func filter(_ predicateFormat: String, _ args: Any...) -> Results<Element>
-
- func filter(_ predicate: NSPredicate) -> Results<Element>
-
-
- func sorted(byKeyPath keyPath: String, ascending: Bool) -> Results<Element>
-
- func sorted<S: Sequence>(by sortDescriptors: S) -> Results<Element> where S.Iterator.Element == SortDescriptor
-
-
- func min<T: MinMaxType>(ofProperty property: String) -> T?
-
- func max<T: MinMaxType>(ofProperty property: String) -> T?
-
- func sum<T: AddableType>(ofProperty property: String) -> T
-
- func average(ofProperty property: String) -> Double?
-
-
- func value(forKey key: String) -> Any?
-
- func value(forKeyPath keyPath: String) -> Any?
-
- func setValue(_ value: Any?, forKey key: String)
-
-
- func observe(_ block: @escaping (RealmCollectionChange<Self>) -> Void) -> NotificationToken
-
- func _observe(_ block: @escaping (RealmCollectionChange<AnyRealmCollection<Element>>) -> Void) -> NotificationToken
- }
- public protocol OptionalProtocol {
- associatedtype Wrapped
-
-
- func _rlmInferWrappedType() -> Wrapped
- }
- extension Optional: OptionalProtocol {
-
-
- public func _rlmInferWrappedType() -> Wrapped { return self! }
- }
- public extension RealmCollection where Element: MinMaxType {
-
- func min() -> Element? {
- return min(ofProperty: "self")
- }
-
- func max() -> Element? {
- return max(ofProperty: "self")
- }
- }
- public extension RealmCollection where Element: OptionalProtocol, Element.Wrapped: MinMaxType {
-
- func min() -> Element.Wrapped? {
- return min(ofProperty: "self")
- }
-
- func max() -> Element.Wrapped? {
- return max(ofProperty: "self")
- }
- }
- public extension RealmCollection where Element: AddableType {
-
- func sum() -> Element {
- return sum(ofProperty: "self")
- }
-
- func average() -> Double? {
- return average(ofProperty: "self")
- }
- }
- public extension RealmCollection where Element: OptionalProtocol, Element.Wrapped: AddableType {
-
- func sum() -> Element.Wrapped {
- return sum(ofProperty: "self")
- }
-
- func average() -> Double? {
- return average(ofProperty: "self")
- }
- }
- public extension RealmCollection where Element: Comparable {
-
- func sorted(ascending: Bool = true) -> Results<Element> {
- return sorted(byKeyPath: "self", ascending: ascending)
- }
- }
- public extension RealmCollection where Element: OptionalProtocol, Element.Wrapped: Comparable {
-
- func sorted(ascending: Bool = true) -> Results<Element> {
- return sorted(byKeyPath: "self", ascending: ascending)
- }
- }
- private class _AnyRealmCollectionBase<T: RealmCollectionValue>: AssistedObjectiveCBridgeable {
- typealias Wrapper = AnyRealmCollection<Element>
- typealias Element = T
- var realm: Realm? { fatalError() }
- var isInvalidated: Bool { fatalError() }
- var count: Int { fatalError() }
- var description: String { fatalError() }
- func index(of object: Element) -> Int? { fatalError() }
- func index(matching predicate: NSPredicate) -> Int? { fatalError() }
- func index(matching predicateFormat: String, _ args: Any...) -> Int? { fatalError() }
- func filter(_ predicateFormat: String, _ args: Any...) -> Results<Element> { fatalError() }
- func filter(_ predicate: NSPredicate) -> Results<Element> { fatalError() }
- func sorted(byKeyPath keyPath: String, ascending: Bool) -> Results<Element> { fatalError() }
- func sorted<S: Sequence>(by sortDescriptors: S) -> Results<Element> where S.Iterator.Element == SortDescriptor {
- fatalError()
- }
- func min<T: MinMaxType>(ofProperty property: String) -> T? { fatalError() }
- func max<T: MinMaxType>(ofProperty property: String) -> T? { fatalError() }
- func sum<T: AddableType>(ofProperty property: String) -> T { fatalError() }
- func average(ofProperty property: String) -> Double? { fatalError() }
- subscript(position: Int) -> Element { fatalError() }
- func makeIterator() -> RLMIterator<T> { fatalError() }
- var startIndex: Int { fatalError() }
- var endIndex: Int { fatalError() }
- func value(forKey key: String) -> Any? { fatalError() }
- func value(forKeyPath keyPath: String) -> Any? { fatalError() }
- func setValue(_ value: Any?, forKey key: String) { fatalError() }
- func _observe(_ block: @escaping (RealmCollectionChange<Wrapper>) -> Void)
- -> NotificationToken { fatalError() }
- class func bridging(from objectiveCValue: Any, with metadata: Any?) -> Self { fatalError() }
- var bridged: (objectiveCValue: Any, metadata: Any?) { fatalError() }
-
- func _asNSFastEnumerator() -> Any { fatalError() }
- }
- private final class _AnyRealmCollection<C: RealmCollection>: _AnyRealmCollectionBase<C.Element> {
- let base: C
- init(base: C) {
- self.base = base
- }
-
- override var realm: Realm? { return base.realm }
- override var isInvalidated: Bool { return base.isInvalidated }
- override var count: Int { return base.count }
- override var description: String { return base.description }
-
- override func index(of object: C.Element) -> Int? { return base.index(of: object) }
- override func index(matching predicate: NSPredicate) -> Int? { return base.index(matching: predicate) }
- override func index(matching predicateFormat: String, _ args: Any...) -> Int? {
- return base.index(matching: NSPredicate(format: predicateFormat, argumentArray: unwrapOptionals(in: args)))
- }
-
- override func filter(_ predicateFormat: String, _ args: Any...) -> Results<C.Element> {
- return base.filter(NSPredicate(format: predicateFormat, argumentArray: unwrapOptionals(in: args)))
- }
- override func filter(_ predicate: NSPredicate) -> Results<C.Element> { return base.filter(predicate) }
-
- override func sorted(byKeyPath keyPath: String, ascending: Bool) -> Results<C.Element> {
- return base.sorted(byKeyPath: keyPath, ascending: ascending)
- }
- override func sorted<S: Sequence>
- (by sortDescriptors: S) -> Results<C.Element> where S.Iterator.Element == SortDescriptor {
- return base.sorted(by: sortDescriptors)
- }
-
- override func min<T: MinMaxType>(ofProperty property: String) -> T? {
- return base.min(ofProperty: property)
- }
- override func max<T: MinMaxType>(ofProperty property: String) -> T? {
- return base.max(ofProperty: property)
- }
- override func sum<T: AddableType>(ofProperty property: String) -> T {
- return base.sum(ofProperty: property)
- }
- override func average(ofProperty property: String) -> Double? {
- return base.average(ofProperty: property)
- }
-
- override subscript(position: Int) -> C.Element {
- return base[position as! C.Index]
- }
- override func makeIterator() -> RLMIterator<Element> {
-
- return base.makeIterator() as! RLMIterator<Element>
- }
-
- override func _asNSFastEnumerator() -> Any {
- return base._asNSFastEnumerator()
- }
-
- override var startIndex: Int {
-
- return base.startIndex as! Int
- }
- override var endIndex: Int {
-
- return base.endIndex as! Int
- }
-
- override func value(forKey key: String) -> Any? { return base.value(forKey: key) }
- override func value(forKeyPath keyPath: String) -> Any? { return base.value(forKeyPath: keyPath) }
- override func setValue(_ value: Any?, forKey key: String) { base.setValue(value, forKey: key) }
-
-
- override func _observe(_ block: @escaping (RealmCollectionChange<Wrapper>) -> Void)
- -> NotificationToken { return base._observe(block) }
-
- override class func bridging(from objectiveCValue: Any, with metadata: Any?) -> _AnyRealmCollection {
- return _AnyRealmCollection(
- base: (C.self as! AssistedObjectiveCBridgeable.Type).bridging(from: objectiveCValue, with: metadata) as! C)
- }
- override var bridged: (objectiveCValue: Any, metadata: Any?) {
- return (base as! AssistedObjectiveCBridgeable).bridged
- }
- }
- public struct AnyRealmCollection<Element: RealmCollectionValue>: RealmCollection {
-
- public typealias ElementType = Element
- public func index(after i: Int) -> Int { return i + 1 }
- public func index(before i: Int) -> Int { return i - 1 }
-
- fileprivate let base: _AnyRealmCollectionBase<Element>
- fileprivate init(base: _AnyRealmCollectionBase<Element>) {
- self.base = base
- }
-
- public init<C: RealmCollection>(_ base: C) where C.Element == Element {
- self.base = _AnyRealmCollection(base: base)
- }
-
-
- public var realm: Realm? { return base.realm }
-
- public var isInvalidated: Bool { return base.isInvalidated }
-
- public var count: Int { return base.count }
-
- public var description: String { return base.description }
-
-
- public func index(of object: Element) -> Int? { return base.index(of: object) }
-
- public func index(matching predicate: NSPredicate) -> Int? { return base.index(matching: predicate) }
-
- public func index(matching predicateFormat: String, _ args: Any...) -> Int? {
- return base.index(matching: NSPredicate(format: predicateFormat, argumentArray: unwrapOptionals(in: args)))
- }
-
-
- public func filter(_ predicateFormat: String, _ args: Any...) -> Results<Element> {
- return base.filter(NSPredicate(format: predicateFormat, argumentArray: unwrapOptionals(in: args)))
- }
-
- public func filter(_ predicate: NSPredicate) -> Results<Element> { return base.filter(predicate) }
-
-
- public func sorted(byKeyPath keyPath: String, ascending: Bool) -> Results<Element> {
- return base.sorted(byKeyPath: keyPath, ascending: ascending)
- }
-
- public func sorted<S: Sequence>(by sortDescriptors: S) -> Results<Element>
- where S.Iterator.Element == SortDescriptor {
- return base.sorted(by: sortDescriptors)
- }
-
-
- public func min<T: MinMaxType>(ofProperty property: String) -> T? {
- return base.min(ofProperty: property)
- }
-
- public func max<T: MinMaxType>(ofProperty property: String) -> T? {
- return base.max(ofProperty: property)
- }
-
- public func sum<T: AddableType>(ofProperty property: String) -> T { return base.sum(ofProperty: property) }
-
- public func average(ofProperty property: String) -> Double? { return base.average(ofProperty: property) }
-
-
- public subscript(position: Int) -> Element { return base[position] }
-
- public func makeIterator() -> RLMIterator<Element> { return base.makeIterator() }
-
-
- public func _asNSFastEnumerator() -> Any { return base._asNSFastEnumerator() }
-
-
-
- public var startIndex: Int { return base.startIndex }
-
-
-
- public var endIndex: Int { return base.endIndex }
-
-
- public func value(forKey key: String) -> Any? { return base.value(forKey: key) }
-
- public func value(forKeyPath keyPath: String) -> Any? { return base.value(forKeyPath: keyPath) }
-
- public func setValue(_ value: Any?, forKey key: String) { base.setValue(value, forKey: key) }
-
-
- public func observe(_ block: @escaping (RealmCollectionChange<AnyRealmCollection>) -> Void)
- -> NotificationToken { return base._observe(block) }
-
- public func _observe(_ block: @escaping (RealmCollectionChange<AnyRealmCollection>) -> Void)
- -> NotificationToken { return base._observe(block) }
- }
- private struct AnyRealmCollectionBridgingMetadata<T: RealmCollectionValue> {
- var baseMetadata: Any?
- var baseType: _AnyRealmCollectionBase<T>.Type
- }
- extension AnyRealmCollection: AssistedObjectiveCBridgeable {
- internal static func bridging(from objectiveCValue: Any, with metadata: Any?) -> AnyRealmCollection {
- guard let metadata = metadata as? AnyRealmCollectionBridgingMetadata<Element> else { preconditionFailure() }
- return AnyRealmCollection(base: metadata.baseType.bridging(from: objectiveCValue, with: metadata.baseMetadata))
- }
- internal var bridged: (objectiveCValue: Any, metadata: Any?) {
- return (
- objectiveCValue: base.bridged.objectiveCValue,
- metadata: AnyRealmCollectionBridgingMetadata(baseMetadata: base.bridged.metadata, baseType: type(of: base))
- )
- }
- }
|