# ObjectMonitor

    @available(OSX 10.12, *)
    public final class ObjectMonitor<D> : Equatable where D : DynamicObject

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 = CoreStore.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.

- `ObjectType`

  The type for the object contained by the `ObjectMonitor`

  #### Declaration

  Swift

      public typealias ObjectType = D

- `object`

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

  #### Declaration

  Swift

      public var object: ObjectType? { 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 D == 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 D == 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<ObjectType>, rhs: ObjectMonitor<ObjectType>) -> Bool

### CustomDebugStringConvertible

- `debugDescription`

  #### Declaration

  Swift

      public var debugDescription: String { get }