ObjectPublisher

@dynamicMemberLookup
public final class ObjectPublisher<O> : ObjectRepresentation, Hashable where O : DynamicObject
extension ObjectPublisher: CustomDebugStringConvertible, CoreStoreDebugStringConvertible

The ObjectPublisher tracks changes to a single DynamicObject instance. Objects that need to be notified of ObjectPublisher changes may register themselves to its addObserver(_:_:) method:

let objectPublisher = CoreStoreDefaults.dataStack.objectPublisher(object)
objectPublisher.addObserver(self) { (objectPublisher) in
    // Handle changes
}

The created ObjectPublisher instance needs to be held on (retained) for as long as the object needs to be observed.

Observers registered via addObserver(_:_:) are not retained. ObjectPublisher only keeps a weak reference to all observers, thus keeping itself free from retain-cycles.

The ObjectPublisher‘s snapshot value is a lazy copy operation that extracts all property values at a specific point time. This cached copy is invalidated right before the ObjectPublisher broadcasts any changes to its observers.

Public (Accessors)

  • A snapshot of the latest state of this list. Returns nil if the object has been deleted.

    Declaration

    Swift

    public var snapshot: ObjectSnapshot<O>? { get }
  • The actual DynamicObject instance. Becomes nil if the object has been deleted.

    Declaration

    Swift

    public private(set) lazy var object: O? { get set }

Public (Observers)

  • Registers an object as an observer to be notified when changes to the ObjectPublisher‘s snapshot occur.

    To prevent retain-cycles, ObjectPublisher 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.

    Declaration

    Swift

    public func addObserver<T: AnyObject>(
        _ observer: T,
        notifyInitial: Bool = false,
        _ callback: @escaping (ObjectPublisher<O>) -> Void
    )

    Parameters

    observer

    an object to become owner of the specified callback

    notifyInitial

    if true, the callback is executed immediately with the current publisher state. Otherwise only succeeding updates will notify the observer. Default value is false.

    callback

    the closure to execute when changes occur

  • Registers an object as an observer to be notified when changes to the ObjectPublisher‘s snapshot occur.

    To prevent retain-cycles, ObjectPublisher 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.

    Declaration

    Swift

    public func addObserver<T: AnyObject>(
        _ observer: T,
        notifyInitial: Bool = false,
        initialSourceIdentifier: Any? = nil,
        _ callback: @escaping (
            _ objectPublisher: ObjectPublisher<O>,
            _ sourceIdentifier: Any?
        ) -> Void
    )

    Parameters

    observer

    an object to become owner of the specified callback

    notifyInitial

    if true, the callback is executed immediately with the current publisher state. Otherwise only succeeding updates will notify the observer. Default value is false.

    initialSourceIdentifier

    an optional value that identifies the initial callback invocation if notifyInitial is true.

    callback

    the closure to execute when changes occur

  • Unregisters an object from receiving notifications for changes to the ObjectPublisher‘s snapshot.

    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<T>(_ observer: T) where T : AnyObject

    Parameters

    observer

    the object whose notifications will be unregistered

AnyObjectRepresentation

ObjectRepresentation

Equatable

  • Declaration

    Swift

    public static func == (lhs: ObjectPublisher, rhs: ObjectPublisher) -> Bool

Hashable

  • Declaration

    Swift

    public func hash(into hasher: inout Hasher)

CustomDebugStringConvertible

  • Declaration

    Swift

    public var debugDescription: String { get }

Public

  • Combine utilities for the ObjectPublisher are exposed through this namespace

    Declaration

    Swift

    public var reactive: ObjectPublisher.ReactiveNamespace { get }

ReactiveNamespace

SnapshotPublisher

  • A Publisher that emits an ObjectSnapshot? whenever changes occur in the ObjectPublisher. The event emits nil if the object has been deletd.

    See also

    ObjectPublisher.reactive.snapshot(emitInitialValue:)
    See more

    Declaration

    Swift

    public struct SnapshotPublisher : Publisher

Public