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, or nil if the object was already deleted.

    Declaration

    Swift

    public var object: O? { get }
  • Returns true if the DynamicObject instance being observed still exists, or false if the object was already deleted.

    Declaration

    Swift

    public var isObjectDeleted: Bool { get }
  • Registers an ObjectObserver to be notified when changes to the receiver’s object are made.

    To prevent retain-cycles, ObjectMonitor 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 addObserver(_:) multiple times on the same observer is safe, as ObjectMonitor 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’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.

    Declaration

    Swift

    public func removeObserver<U>(_ observer: U) where O == U.ObjectEntityType, U : ObjectObserver

    Parameters

    observer

    an ObjectObserver to unregister notifications to

Public (3rd Party Utilities)

  • 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

Equatable

  • Declaration

    Swift

    public static func == (lhs: ObjectMonitor<O>, rhs: ObjectMonitor<O>) -> Bool

Hashable

  • Declaration

    Swift

    public func hash(into hasher: inout Hasher)

AnyObjectRepresentation

ObjectRepresentation

CustomDebugStringConvertible

  • Declaration

    Swift

    public var debugDescription: String { get }