ListPublisher
public final class ListPublisher<O> : Hashable where O : DynamicObject
extension ListPublisher: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
ListPublisher tracks a diffable list of DynamicObject instances. Unlike ListMonitors, ListPublisher are more lightweight and access objects lazily. Objects that need to be notified of ListPublisher changes may register themselves to its addObserver(_:_:) method:
let listPublisher = CoreStoreDefaults.dataStack.listPublisher(
From<Person>()
.where(\.title == "Engineer")
.orderBy(.ascending(\.lastName))
)
listPublisher.addObserver(self) { (listPublisher) in
// Handle changes
}
The ListPublisher instance needs to be held on (retained) for as long as the list needs to be observed.
Observers registered via addObserver(_:_:) are not retained. ListPublisher only keeps a weak reference to all observers, thus keeping itself free from retain-cycles.
ListPublishers may optionally be created with sections:
let listPublisher = CoreStoreDefaults.dataStack.listPublisher(
From<Person>()
.sectionBy(\.age") { "Age \($0)" }
.where(\.title == "Engineer")
.orderBy(.ascending(\.lastName))
)
All access to the ListPublisher items should be done via its snapshot value, which is a struct of type ListSnapshot<O>. ListSnapshots are also designed to work well with DiffableDataSource.TableViewAdapters and DiffableDataSource.CollectionViewAdapters. For detailed examples, refer to the documentation for DiffableDataSource.TableViewAdapter and DiffableDataSource.CollectionViewAdapter.
-
The
DynamicObjecttype associated with this listDeclaration
Swift
public typealias ObjectType = O -
The type for the section IDs
Declaration
Swift
public typealias SectionID = ListSnapshot<O>.SectionID -
The type for the item IDs
Declaration
Swift
public typealias ItemID = ListSnapshot<O>.ItemID -
A snapshot of the latest state of this list
Declaration
Swift
public private(set) var snapshot: ListSnapshot<O> { get }
-
Registers an object as an observer to be notified when changes to the
ListPublisher‘s snapshot occur.To prevent retain-cycles,
ListPublisheronly keepsweakreferences 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 (ListPublisher<O>) -> Void )Parameters
observeran object to become owner of the specified
callbacknotifyInitialif
true, the callback is executed immediately with the current publisher state. Otherwise only succeeding updates will notify the observer. Default value isfalse.callbackthe closure to execute when changes occur
-
Registers an object as an observer to be notified when changes to the
ListPublisher‘s snapshot occur.To prevent retain-cycles,
ListPublisheronly keepsweakreferences 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 ( _ listPublisher: ListPublisher<O>, _ sourceIdentifier: Any? ) -> Void )Parameters
observeran object to become owner of the specified
callbacknotifyInitialif
true, the callback is executed immediately with the current publisher state. Otherwise only succeeding updates will notify the observer. Default value isfalse.initialSourceIdentifieran optional value that identifies the initial callback invocation if
notifyInitialistrue.callbackthe closure to execute when changes occur
-
Unregisters an object from receiving notifications for changes to the
ListPublisher‘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 : AnyObjectParameters
observerthe object whose notifications will be unregistered
-
Asks the
ListPublisherto refetch its objects using the specifiedFetchChainableBuilderType. UnlikeListMonitors, aListPublisher‘srefetch(...)executes immediately.try listPublisher.refetch( From<MyPersonEntity>() .where(\.age > 18) .orderBy(.ascending(\.age)) )Declaration
Swift
public func refetch<B: FetchChainableBuilderType>( _ clauseChain: B, sourceIdentifier: Any? = nil ) throws where B.ObjectType == OParameters
clauseChaina
FetchChainableBuilderTypebuilt from a chain of clausessourceIdentifieran optional value that identifies the source of this transaction. This identifier will be passed to the change notifications and callers can use it for custom handling that depends on the source.
-
Asks the
ListPublisherto refetch its objects using the specifiedSectionMonitorBuilderType. UnlikeListMonitors, aListPublisher‘srefetch(...)executes immediately.try listPublisher.refetch( From<MyPersonEntity>() .sectionBy(\.age, { "\($0!) years old" }) .where(\.age > 18) .orderBy(.ascending(\.age)) )Declaration
Swift
public func refetch<B: SectionMonitorBuilderType>( _ clauseChain: B, sourceIdentifier: Any? = nil ) throws where B.ObjectType == OParameters
clauseChaina
SectionMonitorBuilderTypebuilt from a chain of clausessourceIdentifieran optional value that identifies the source of this transaction. This identifier will be passed to the change notifications and callers can use it for custom handling that depends on the source.
-
Used internally by CoreStore. Do not call directly.
Declaration
Swift
public func cs_dataStack() -> DataStack?
-
Allow external libraries to store custom data in the
ListPublisher. App code should rarely have a need for this.enum Static { static var myDataKey: Void? } monitor.userInfo[&Static.myDataKey] = myObjectImportant
Do not use this method to store thread-sensitive data.Declaration
Swift
public let userInfo: UserInfo
-
Declaration
Swift
public static func == (lhs: ListPublisher, rhs: ListPublisher) -> Bool
-
Declaration
Swift
public func hash(into hasher: inout Hasher)
-
Declaration
Swift
public var debugDescription: String { get }
-
Combine utilities for the
ListPublisherare exposed through this namespaceDeclaration
Swift
public var reactive: ListPublisher.ReactiveNamespace { get }
-
Combine utilities for the
See moreListPublisherare exposed through this namespace. Extend this type if you need to add other Combine Publisher utilities forListPublisher.Declaration
Swift
public struct ReactiveNamespace
-
A
Publisherthat emits aListSnapshotwhenever changes occur in theListPublisher.See moreSee also
ListPublisher.reactive.snapshot(emitInitialValue:)Declaration
Swift
public struct SnapshotPublisher : Publisher
View on GitHub
ListPublisher Class Reference