ListMonitor
public final class ListMonitor<O> : Hashable where O : DynamicObject
extension ListMonitor: CustomDebugStringConvertible, CoreStoreDebugStringConvertible
The ListMonitor monitors changes to a list of DynamicObject instances. Observers that implement the ListObserver protocol may then register themselves to the ListMonitor‘s addObserver(_:) method:
let monitor = dataStack.monitorList(
From<Person>(),
Where("title", isEqualTo: "Engineer"),
OrderBy(.ascending("lastName"))
)
monitor.addObserver(self)
The ListMonitor instance needs to be held on (retained) for as long as the list needs to be observed.
Observers registered via addObserver(_:) are not retained. ListMonitor only keeps a weak reference to all observers, thus keeping itself free from retain-cycles.
Lists created with monitorList(...) keep a single-section list of objects, where each object can be accessed by index:
let firstPerson: MyPersonEntity = monitor[0]
Accessing the list with an index above the valid range will raise an exception.
Creating a sectioned-list is also possible with the monitorSectionedList(...) method:
let monitor = dataStack.monitorSectionedList(
From<Person>(),
SectionBy("age") { "Age \($0)" },
Where("title", isEqualTo: "Engineer"),
OrderBy(.ascending("lastName"))
)
monitor.addObserver(self)
Objects from ListMonitors created this way can be accessed either by an IndexPath or a tuple:
let indexPath = IndexPath(forItem: 3, inSection: 2)
let person1 = monitor[indexPath]
let person2 = monitor[2, 3]
In the example above, both person1 and person2 will contain the object at section=2, index=3.
-
The type for the objects contained bye the
ListMonitorDeclaration
Swift
public typealias ObjectType = O -
Returns the object at the given index within the first section. This subscript indexer is typically used for
ListMonitors created withmonitorList(_:).Declaration
Swift
public subscript(index: Int) -> O { get }Parameters
indexthe index of the object. Using an index above the valid range will raise an exception.
Return Value
the
DynamicObjectat the specified index -
Returns the object at the given index, or
nilif out of bounds. This subscript indexer is typically used forListMonitors created withmonitorList(_:).Declaration
Swift
public subscript(safeIndex index: Int) -> O? { get }Parameters
indexthe index for the object. Using an index above the valid range will return
nil.Return Value
the
DynamicObjectat the specified index, ornilif out of bounds -
Returns the object at the given
sectionIndexanditemIndex. This subscript indexer is typically used forListMonitors created withmonitorSectionedList(_:).Declaration
Swift
public subscript(sectionIndex: Int, itemIndex: Int) -> O { get }Parameters
sectionIndexthe section index for the object. Using a
sectionIndexwith an invalid range will raise an exception.itemIndexthe index for the object within the section. Using an
itemIndexwith an invalid range will raise an exception.Return Value
the
DynamicObjectat the specified section and item index -
Returns the object at the given section and item index, or
nilif out of bounds. This subscript indexer is typically used forListMonitors created withmonitorSectionedList(_:).Declaration
Swift
public subscript(safeSectionIndex sectionIndex: Int, safeItemIndex itemIndex: Int) -> O? { get }Parameters
sectionIndexthe section index for the object. Using a
sectionIndexwith an invalid range will returnnil.itemIndexthe index for the object within the section. Using an
itemIndexwith an invalid range will returnnil.Return Value
the
DynamicObjectat the specified section and item index, ornilif out of bounds -
Returns the object at the given
IndexPath. This subscript indexer is typically used forListMonitors created withmonitorSectionedList(_:).Declaration
Swift
public subscript(indexPath: IndexPath) -> O { get }Parameters
indexPaththe
IndexPathfor the object. Using anindexPathwith an invalid range will raise an exception.Return Value
the
DynamicObjectat the specified index path -
Returns the object at the given
IndexPath, ornilif out of bounds. This subscript indexer is typically used forListMonitors created withmonitorSectionedList(_:).Declaration
Swift
public subscript(safeIndexPath indexPath: IndexPath) -> O? { get }Parameters
indexPaththe
IndexPathfor the object. Using anindexPathwith an invalid range will returnnil.Return Value
the
DynamicObjectat the specified index path, ornilif out of bounds -
Checks if the
ListMonitorhas at least one sectionDeclaration
Swift
public func hasSections() -> BoolReturn Value
trueif at least one section exists,falseotherwise -
Checks if the
ListMonitorhas at least one object in any section.Declaration
Swift
public func hasObjects() -> BoolReturn Value
trueif at least one object in any section exists,falseotherwise -
Checks if the
ListMonitorhas at least one object the specified section.Declaration
Swift
public func hasObjects(in section: Int) -> BoolParameters
sectionthe section index. Using an index outside the valid range will return
false.Return Value
trueif at least one object in the specified section exists,falseotherwise -
Returns the number of sections
Declaration
Swift
public func numberOfSections() -> IntReturn Value
the number of sections
-
Returns the number of objects in all sections
Declaration
Swift
public func numberOfObjects() -> IntReturn Value
the number of objects in all sections
-
Returns the number of objects in the specified section
Declaration
Swift
public func numberOfObjects(in section: Int) -> IntParameters
sectionthe section index. Using an index outside the valid range will raise an exception.
Return Value
the number of objects in the specified section
-
Returns the number of objects in the specified section, or
nilif out of bounds.Declaration
Swift
public func numberOfObjects(safelyIn section: Int) -> Int?Parameters
sectionthe section index. Using an index outside the valid range will return
nil.Return Value
the number of objects in the specified section
-
Returns the
NSFetchedResultsSectionInfofor the specified sectionDeclaration
Swift
public func sectionInfo(at section: Int) -> NSFetchedResultsSectionInfoParameters
sectionthe section index. Using an index outside the valid range will raise an exception.
Return Value
the
NSFetchedResultsSectionInfofor the specified section -
Returns the
NSFetchedResultsSectionInfofor the specified section, ornilif out of bounds.Declaration
Swift
public func sectionInfo(safelyAt section: Int) -> NSFetchedResultsSectionInfo?Parameters
sectionthe section index. Using an index outside the valid range will return
nil.Return Value
the
NSFetchedResultsSectionInfofor the specified section, ornilif the section index is out of bounds. -
Returns the
NSFetchedResultsSectionInfos for all sectionsDeclaration
Swift
public func sections() -> [NSFetchedResultsSectionInfo]Return Value
the
NSFetchedResultsSectionInfos for all sections -
Returns the target section for a specified “Section Index” title and index.
Declaration
Swift
public func targetSection(forSectionIndexTitle sectionIndexTitle: String, at sectionIndex: Int) -> IntParameters
sectionIndexTitlethe title of the Section Index
sectionIndexthe index of the Section Index
Return Value
the target section for the specified “Section Index” title and index.
-
Returns the section index titles for all sections
Declaration
Swift
public func sectionIndexTitles() -> [String]Return Value
the section index titles for all sections
-
Returns the index of the
DynamicObjectif it exists in theListMonitor‘s fetched objects, ornilif not found.Declaration
Swift
public func index(of object: O) -> Int?Parameters
objectthe
DynamicObjectto search the index ofReturn Value
the index of the
DynamicObjectif it exists in theListMonitor‘s fetched objects, ornilif not found. -
Returns the
IndexPathof theDynamicObjectif it exists in theListMonitor‘s fetched objects, ornilif not found.Declaration
Swift
public func indexPath(of object: O) -> IndexPath?Parameters
objectthe
DynamicObjectto search the index ofReturn Value
the
IndexPathof theDynamicObjectif it exists in theListMonitor‘s fetched objects, ornilif not found.
-
Registers a
ListObserverto be notified when changes to the receiver’s list occur.To prevent retain-cycles,
ListMonitoronly 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, asListMonitorunregisters previous notifications to the observer before re-registering them.Declaration
Swift
public func addObserver<U>(_ observer: U) where O == U.ListEntityType, U : ListObserverParameters
observera
ListObserverto send change notifications to -
Registers a
ListObjectObserverto be notified when changes to the receiver’s list occur.To prevent retain-cycles,
ListMonitoronly 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, asListMonitorunregisters previous notifications to the observer before re-registering them.Declaration
Swift
public func addObserver<U>(_ observer: U) where O == U.ListEntityType, U : ListObjectObserverParameters
observera
ListObjectObserverto send change notifications to -
Registers a
ListSectionObserverto be notified when changes to the receiver’s list occur.To prevent retain-cycles,
ListMonitoronly 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, asListMonitorunregisters previous notifications to the observer before re-registering them.Declaration
Swift
public func addObserver<U>(_ observer: U) where O == U.ListEntityType, U : ListSectionObserverParameters
observera
ListSectionObserverto send change notifications to -
Unregisters a
ListObserverfrom receiving notifications for changes to the receiver’s list.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.ListEntityType, U : ListObserverParameters
observera
ListObserverto unregister notifications to
-
Returns
trueif a call torefetch(...)was made to theListMonitorand is currently waiting for the fetching to complete. Returnsfalseotherwise.Declaration
Swift
public private(set) var isPendingRefetch: Bool { get } -
Asks the
ListMonitorto refetch its objects using the specified series ofFetchClauses. Note that this method does not execute the fetch immediately; the actual fetching will happen after theNSFetchedResultsController‘s lastcontrollerDidChangeContent(_:)notification completes.refetch(...)broadcastslistMonitorWillRefetch(...)to its observers immediately, and thenlistMonitorDidRefetch(...)after the new fetch request completes.Important
Starting CoreStore 4.0, allFetchClauses required by theListMonitorshould be provided in the arguments list ofrefetch(...).Declaration
Swift
public func refetch( _ fetchClauses: FetchClause..., sourceIdentifier: Any? = nil )Parameters
fetchClausesa series of
FetchClauseinstances for fetching the object list. AcceptsWhere,OrderBy, andTweakclauses.sourceIdentifieran 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
ListMonitorto refetch its objects using the specified series ofFetchClauses. Note that this method does not execute the fetch immediately; the actual fetching will happen after theNSFetchedResultsController‘s lastcontrollerDidChangeContent(_:)notification completes.refetch(...)broadcastslistMonitorWillRefetch(...)to its observers immediately, and thenlistMonitorDidRefetch(...)after the new fetch request completes.Important
Starting CoreStore 4.0, allFetchClauses required by theListMonitorshould be provided in the arguments list ofrefetch(...).Declaration
Swift
public func refetch( _ fetchClauses: [FetchClause], sourceIdentifier: Any? = nil )Parameters
fetchClausesa series of
FetchClauseinstances for fetching the object list. AcceptsWhere,OrderBy, andTweakclauses.sourceIdentifieran 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.
-
Allow external libraries to store custom data in the
ListMonitor. 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: ListMonitor<O>, rhs: ListMonitor<O>) -> Bool
-
Declaration
Swift
public func hash(into hasher: inout Hasher)
-
Declaration
Swift
public var debugDescription: String { get }
-
Returns all objects in all sections
Declaration
Swift
public func objectsInAllSections() -> [O]Return Value
all objects in all sections
-
Returns all objects in the specified section
Declaration
Swift
public func objects(in section: Int) -> [O]Parameters
sectionthe section index. Using an index outside the valid range will raise an exception.
Return Value
all objects in the specified section
-
Returns all objects in the specified section, or
nilif out of bounds.Declaration
Swift
public func objects(safelyIn section: Int) -> [O]?Parameters
sectionthe section index. Using an index outside the valid range will return
nil.Return Value
all objects in the specified section
-
Returns all objects in all sections
Declaration
Swift
public func objectsInAllSections() -> [O]Return Value
all objects in all sections
-
Returns all objects in the specified section
Declaration
Swift
public func objects(in section: Int) -> [O]Parameters
sectionthe section index. Using an index outside the valid range will raise an exception.
Return Value
all objects in the specified section
-
Returns all objects in the specified section, or
nilif out of bounds.Declaration
Swift
public func objects(safelyIn section: Int) -> [O]?Parameters
sectionthe section index. Using an index outside the valid range will return
nil.Return Value
all objects in the specified section
View on GitHub
ListMonitor Class Reference