# 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.

- `object`

  Returns the `DynamicObject` instance being observed, or `nil` if the object was already deleted.

  #### Declaration

  Swift

      public var object: O? { get }

- `isObjectDeleted`

  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 }

- `addObserver(_:)`

  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

- `removeObserver(_:)`

  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)

- `userInfo`

  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

- `hash(into:)`

  #### Declaration

  Swift

      public func hash(into hasher: inout Hasher)

### AnyObjectRepresentation

- `objectID()`

  #### Declaration

  Swift

      public func objectID() -> O.ObjectID

- `cs_dataStack()`

  #### Declaration

  Swift

      public func cs_dataStack() -> DataStack?

### ObjectRepresentation

- `ObjectType`

  #### Declaration

  Swift

      public typealias ObjectType = O

- `asPublisher(in:)`

  #### Declaration

  Swift

      public func asPublisher(in dataStack: DataStack) -> ObjectPublisher<O>

- `asReadOnly(in:)`

  #### Declaration

  Swift

      public func asReadOnly(in dataStack: DataStack) -> O?

- `asEditable(in:)`

  #### Declaration

  Swift

      public func asEditable(in transaction: BaseDataTransaction) -> O?

- `asSnapshot(in:)`

  #### Declaration

  Swift

      public func asSnapshot(in dataStack: DataStack) -> ObjectSnapshot<O>?

- `asSnapshot(in:)`

  #### Declaration

  Swift

      public func asSnapshot(in transaction: BaseDataTransaction) -> ObjectSnapshot<O>?

### CustomDebugStringConvertible

- `debugDescription`

  #### Declaration

  Swift

      public var debugDescription: String { get }