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
DynamicObject
instance being observed, ornil
if the object was already deleted.Declaration
Swift
public var object: O? { get }
-
Returns
true
if theDynamicObject
instance being observed still exists, orfalse
if the object was already deleted.Declaration
Swift
public var isObjectDeleted: Bool { get }
-
Registers an
ObjectObserver
to be notified when changes to the receiver’sobject
are made.To prevent retain-cycles,
ObjectMonitor
only keepsweak
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
addObserver(_:)
multiple times on the same observer is safe, asObjectMonitor
unregisters previous notifications to the observer before re-registering them.Declaration
Swift
public func addObserver<U>(_ observer: U) where O == U.ObjectEntityType, U : ObjectObserver
Parameters
observer
an
ObjectObserver
to send change notifications to -
Unregisters an
ObjectObserver
from 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 : ObjectObserver
Parameters
observer
an
ObjectObserver
to 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] = myObject
Important
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 }