ObjectMonitor
public final class ObjectMonitor<O> : Hashable, ObjectRepresentation where O : DynamicObject
extension ObjectMonitor: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
The ObjectMonitor monitors changes to a single DynamicObject instance. Observers that implement the ObjectObserver protocol may then register themselves to the ObjectMonitor‘s addObserver(_:) method:
let monitor = dataStack.monitorObject(object)
monitor.addObserver(self)
The created ObjectMonitor instance needs to be held on (retained) for as long as the object needs to be observed.
Observers registered via addObserver(_:) are not retained. ObjectMonitor only keeps a weak reference to all observers, thus keeping itself free from retain-cycles.
-
Returns the
DynamicObjectinstance being observed, ornilif the object was already deleted.Declaration
Swift
public var object: O? { get } -
Returns
trueif theDynamicObjectinstance being observed still exists, orfalseif the object was already deleted.Declaration
Swift
public var isObjectDeleted: Bool { get } -
Registers an
ObjectObserverto be notified when changes to the receiver’sobjectare made.To prevent retain-cycles,
ObjectMonitoronly keepsweakreferences 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
addObserver(_:)multiple times on the same observer is safe, asObjectMonitorunregisters previous notifications to the observer before re-registering them.Declaration
Swift
public func addObserver<U>(_ observer: U) where O == U.ObjectEntityType, U : ObjectObserverParameters
observeran
ObjectObserverto send change notifications to -
Unregisters an
ObjectObserverfrom receiving notifications for changes to the receiver’sobject.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.
Declaration
Swift
public func removeObserver<U>(_ observer: U) where O == U.ObjectEntityType, U : ObjectObserverParameters
observeran
ObjectObserverto unregister notifications to
-
Allow external libraries to store custom data in the
ObjectMonitor. App code should rarely have a need for this.enum Static { static var myDataKey: Void? } monitor.userInfo[&Static.myDataKey] = myObjectImportant
Do not use this method to store thread-sensitive data.Declaration
Swift
public let userInfo: UserInfo
-
Declaration
Swift
public static func == (lhs: ObjectMonitor<O>, rhs: ObjectMonitor<O>) -> Bool
-
Declaration
Swift
public func hash(into hasher: inout Hasher)
-
Declaration
Swift
public func objectID() -> O.ObjectID -
Declaration
Swift
public func cs_dataStack() -> DataStack?
-
Declaration
Swift
public typealias ObjectType = O -
Declaration
Swift
public func asPublisher(in dataStack: DataStack) -> ObjectPublisher<O> -
Declaration
Swift
public func asReadOnly(in dataStack: DataStack) -> O? -
Declaration
Swift
public func asEditable(in transaction: BaseDataTransaction) -> O? -
Declaration
Swift
public func asSnapshot(in dataStack: DataStack) -> ObjectSnapshot<O>? -
Declaration
Swift
public func asSnapshot(in transaction: BaseDataTransaction) -> ObjectSnapshot<O>?
-
Declaration
Swift
public var debugDescription: String { get }
View on GitHub
ObjectMonitor Class Reference