mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-26 03:11:14 +01:00
goodbye ObjectiveC
This commit is contained in:
@@ -29,72 +29,28 @@ import CoreData
|
||||
|
||||
// MARK: - CSObjectMonitor
|
||||
|
||||
/**
|
||||
The `CSObjectMonitor` serves as the Objective-C bridging type for `ObjectMonitor<T>`.
|
||||
|
||||
- SeeAlso: `ObjectMonitor`
|
||||
*/
|
||||
@available(*, unavailable, message: "CoreStore Objective-C is now obsoleted in preparation for Swift concurrency.")
|
||||
@objc
|
||||
public final class CSObjectMonitor: NSObject {
|
||||
|
||||
/**
|
||||
Returns the `NSManagedObject` instance being observed, or `nil` if the object was already deleted.
|
||||
*/
|
||||
public var object: Any? {
|
||||
|
||||
return self.bridgeToSwift.object
|
||||
|
||||
fatalError()
|
||||
}
|
||||
|
||||
/**
|
||||
Returns `YES` if the `NSManagedObject` instance being observed still exists, or `NO` if the object was already deleted.
|
||||
*/
|
||||
|
||||
public var isObjectDeleted: Bool {
|
||||
|
||||
return self.bridgeToSwift.isObjectDeleted
|
||||
|
||||
fatalError()
|
||||
}
|
||||
|
||||
/**
|
||||
Registers a `CSObjectObserver` to be notified when changes to the receiver's `object` are made.
|
||||
|
||||
To prevent retain-cycles, `CSObjectMonitor` only keeps `weak` references to its observers.
|
||||
|
||||
For thread safety, this method needs to be called from the main thread. An assertion failure will occur (on debug builds only) if called from any thread other than the main thread.
|
||||
|
||||
Calling `-addObjectObserver:` multiple times on the same observer is safe, as `CSObjectMonitor` unregisters previous notifications to the observer before re-registering them.
|
||||
|
||||
- parameter observer: an `CSObjectObserver` to send change notifications to
|
||||
*/
|
||||
|
||||
public func addObjectObserver(_ observer: CSObjectObserver) {
|
||||
|
||||
let swift = self.bridgeToSwift
|
||||
swift.unregisterObserver(observer)
|
||||
swift.registerObserver(
|
||||
observer,
|
||||
willChangeObject: { (observer, monitor, object) in
|
||||
|
||||
observer.objectMonitor?(monitor.bridgeToObjectiveC, willUpdateObject: object)
|
||||
},
|
||||
didDeleteObject: { (observer, monitor, object) in
|
||||
|
||||
observer.objectMonitor?(monitor.bridgeToObjectiveC, didDeleteObject: object)
|
||||
},
|
||||
didUpdateObject: { (observer, monitor, object, changedPersistentKeys) in
|
||||
|
||||
observer.objectMonitor?(monitor.bridgeToObjectiveC, didUpdateObject: object, changedPersistentKeys: changedPersistentKeys)
|
||||
}
|
||||
)
|
||||
|
||||
fatalError()
|
||||
}
|
||||
|
||||
/**
|
||||
Unregisters an `CSObjectObserver` from receiving notifications for changes to the receiver's `object`.
|
||||
|
||||
For thread safety, this method needs to be called from the main thread. An assertion failure will occur (on debug builds only) if called from any thread other than the main thread.
|
||||
|
||||
- parameter observer: an `CSObjectObserver` to unregister notifications to
|
||||
*/
|
||||
|
||||
public func removeObjectObserver(_ observer: CSObjectObserver) {
|
||||
|
||||
self.bridgeToSwift.unregisterObserver(observer)
|
||||
|
||||
fatalError()
|
||||
}
|
||||
|
||||
|
||||
@@ -102,23 +58,17 @@ public final class CSObjectMonitor: NSObject {
|
||||
|
||||
public override var hash: Int {
|
||||
|
||||
var hasher = Hasher()
|
||||
self.bridgeToSwift.hash(into: &hasher)
|
||||
return hasher.finalize()
|
||||
fatalError()
|
||||
}
|
||||
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
guard let object = object as? CSObjectMonitor else {
|
||||
|
||||
return false
|
||||
}
|
||||
return self.bridgeToSwift == object.bridgeToSwift
|
||||
|
||||
fatalError()
|
||||
}
|
||||
|
||||
public override var description: String {
|
||||
|
||||
return "(\(String(reflecting: Self.self))) \(self.bridgeToSwift.coreStoreDumpString)"
|
||||
|
||||
fatalError()
|
||||
}
|
||||
|
||||
|
||||
@@ -129,33 +79,21 @@ public final class CSObjectMonitor: NSObject {
|
||||
|
||||
@nonobjc
|
||||
public required init<T: NSManagedObject>(_ swiftValue: ObjectMonitor<T>) {
|
||||
|
||||
self.bridgeToSwift = swiftValue.downcast()
|
||||
super.init()
|
||||
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - ObjectMonitor
|
||||
|
||||
@available(*, unavailable, message: "CoreStore Objective-C is now obsoleted in preparation for Swift concurrency.")
|
||||
extension ObjectMonitor where ObjectMonitor.ObjectType: NSManagedObject {
|
||||
|
||||
// MARK: CoreStoreSwiftType
|
||||
|
||||
public var bridgeToObjectiveC: CSObjectMonitor {
|
||||
|
||||
return CSObjectMonitor(self)
|
||||
}
|
||||
|
||||
|
||||
// MARK: FilePrivate
|
||||
|
||||
fileprivate func downcast() -> ObjectMonitor<NSManagedObject> {
|
||||
|
||||
func noWarnUnsafeBitCast<T, U>(_ x: T, to type: U.Type) -> U {
|
||||
|
||||
return unsafeBitCast(x, to: type)
|
||||
}
|
||||
return noWarnUnsafeBitCast(self, to: ObjectMonitor<NSManagedObject>.self)
|
||||
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user