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 ListMonitor
s 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
ListMonitor
Declaration
Swift
public typealias ObjectType = O
-
Returns the object at the given index within the first section. This subscript indexer is typically used for
ListMonitor
s created withmonitorList(_:)
.Declaration
Swift
public subscript(index: Int) -> O { get }
Parameters
index
the index of the object. Using an index above the valid range will raise an exception.
Return Value
the
DynamicObject
at the specified index -
Returns the object at the given index, or
nil
if out of bounds. This subscript indexer is typically used forListMonitor
s created withmonitorList(_:)
.Declaration
Swift
public subscript(safeIndex index: Int) -> O? { get }
Parameters
index
the index for the object. Using an index above the valid range will return
nil
.Return Value
the
DynamicObject
at the specified index, ornil
if out of bounds -
Returns the object at the given
sectionIndex
anditemIndex
. This subscript indexer is typically used forListMonitor
s created withmonitorSectionedList(_:)
.Declaration
Swift
public subscript(sectionIndex: Int, itemIndex: Int) -> O { get }
Parameters
sectionIndex
the section index for the object. Using a
sectionIndex
with an invalid range will raise an exception.itemIndex
the index for the object within the section. Using an
itemIndex
with an invalid range will raise an exception.Return Value
the
DynamicObject
at the specified section and item index -
Returns the object at the given section and item index, or
nil
if out of bounds. This subscript indexer is typically used forListMonitor
s created withmonitorSectionedList(_:)
.Declaration
Swift
public subscript(safeSectionIndex sectionIndex: Int, safeItemIndex itemIndex: Int) -> O? { get }
Parameters
sectionIndex
the section index for the object. Using a
sectionIndex
with an invalid range will returnnil
.itemIndex
the index for the object within the section. Using an
itemIndex
with an invalid range will returnnil
.Return Value
the
DynamicObject
at the specified section and item index, ornil
if out of bounds -
Returns the object at the given
IndexPath
. This subscript indexer is typically used forListMonitor
s created withmonitorSectionedList(_:)
.Declaration
Swift
public subscript(indexPath: IndexPath) -> O { get }
Parameters
indexPath
the
IndexPath
for the object. Using anindexPath
with an invalid range will raise an exception.Return Value
the
DynamicObject
at the specified index path -
Returns the object at the given
IndexPath
, ornil
if out of bounds. This subscript indexer is typically used forListMonitor
s created withmonitorSectionedList(_:)
.Declaration
Swift
public subscript(safeIndexPath indexPath: IndexPath) -> O? { get }
Parameters
indexPath
the
IndexPath
for the object. Using anindexPath
with an invalid range will returnnil
.Return Value
the
DynamicObject
at the specified index path, ornil
if out of bounds -
Checks if the
ListMonitor
has at least one sectionDeclaration
Swift
public func hasSections() -> Bool
Return Value
true
if at least one section exists,false
otherwise -
Checks if the
ListMonitor
has at least one object in any section.Declaration
Swift
public func hasObjects() -> Bool
Return Value
true
if at least one object in any section exists,false
otherwise -
Checks if the
ListMonitor
has at least one object the specified section.Declaration
Swift
public func hasObjects(in section: Int) -> Bool
Parameters
section
the section index. Using an index outside the valid range will return
false
.Return Value
true
if at least one object in the specified section exists,false
otherwise -
Returns the number of sections
Declaration
Swift
public func numberOfSections() -> Int
Return Value
the number of sections
-
Returns the number of objects in all sections
Declaration
Swift
public func numberOfObjects() -> Int
Return 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) -> Int
Parameters
section
the 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
nil
if out of bounds.Declaration
Swift
public func numberOfObjects(safelyIn section: Int) -> Int?
Parameters
section
the section index. Using an index outside the valid range will return
nil
.Return Value
the number of objects in the specified section
-
Returns the
NSFetchedResultsSectionInfo
for the specified sectionDeclaration
Swift
public func sectionInfo(at section: Int) -> NSFetchedResultsSectionInfo
Parameters
section
the section index. Using an index outside the valid range will raise an exception.
Return Value
the
NSFetchedResultsSectionInfo
for the specified section -
Returns the
NSFetchedResultsSectionInfo
for the specified section, ornil
if out of bounds.Declaration
Swift
public func sectionInfo(safelyAt section: Int) -> NSFetchedResultsSectionInfo?
Parameters
section
the section index. Using an index outside the valid range will return
nil
.Return Value
the
NSFetchedResultsSectionInfo
for the specified section, ornil
if the section index is out of bounds. -
Returns the
NSFetchedResultsSectionInfo
s for all sectionsDeclaration
Swift
public func sections() -> [NSFetchedResultsSectionInfo]
Return Value
the
NSFetchedResultsSectionInfo
s 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) -> Int
Parameters
sectionIndexTitle
the title of the Section Index
sectionIndex
the 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
DynamicObject
if it exists in theListMonitor
‘s fetched objects, ornil
if not found.Declaration
Swift
public func index(of object: O) -> Int?
Parameters
object
the
DynamicObject
to search the index ofReturn Value
the index of the
DynamicObject
if it exists in theListMonitor
‘s fetched objects, ornil
if not found. -
Returns the
IndexPath
of theDynamicObject
if it exists in theListMonitor
‘s fetched objects, ornil
if not found.Declaration
Swift
public func indexPath(of object: O) -> IndexPath?
Parameters
object
the
DynamicObject
to search the index ofReturn Value
the
IndexPath
of theDynamicObject
if it exists in theListMonitor
‘s fetched objects, ornil
if not found.
-
Registers a
ListObserver
to be notified when changes to the receiver’s list occur.To prevent retain-cycles,
ListMonitor
only keepsweak
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, asListMonitor
unregisters previous notifications to the observer before re-registering them.Declaration
Swift
public func addObserver<U>(_ observer: U) where O == U.ListEntityType, U : ListObserver
Parameters
observer
a
ListObserver
to send change notifications to -
Registers a
ListObjectObserver
to be notified when changes to the receiver’s list occur.To prevent retain-cycles,
ListMonitor
only keepsweak
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, asListMonitor
unregisters previous notifications to the observer before re-registering them.Declaration
Swift
public func addObserver<U>(_ observer: U) where O == U.ListEntityType, U : ListObjectObserver
Parameters
observer
a
ListObjectObserver
to send change notifications to -
Registers a
ListSectionObserver
to be notified when changes to the receiver’s list occur.To prevent retain-cycles,
ListMonitor
only keepsweak
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, asListMonitor
unregisters previous notifications to the observer before re-registering them.Declaration
Swift
public func addObserver<U>(_ observer: U) where O == U.ListEntityType, U : ListSectionObserver
Parameters
observer
a
ListSectionObserver
to send change notifications to -
Unregisters a
ListObserver
from 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 : ListObserver
Parameters
observer
a
ListObserver
to unregister notifications to
-
Returns
true
if a call torefetch(...)
was made to theListMonitor
and is currently waiting for the fetching to complete. Returnsfalse
otherwise.Declaration
Swift
public private(set) var isPendingRefetch: Bool { get }
-
Asks the
ListMonitor
to refetch its objects using the specified series ofFetchClause
s. 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, allFetchClause
s required by theListMonitor
should be provided in the arguments list ofrefetch(...)
.Declaration
Swift
public func refetch( _ fetchClauses: FetchClause..., sourceIdentifier: Any? = nil )
Parameters
fetchClauses
a series of
FetchClause
instances for fetching the object list. AcceptsWhere
,OrderBy
, andTweak
clauses.sourceIdentifier
an 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
ListMonitor
to refetch its objects using the specified series ofFetchClause
s. 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, allFetchClause
s required by theListMonitor
should be provided in the arguments list ofrefetch(...)
.Declaration
Swift
public func refetch( _ fetchClauses: [FetchClause], sourceIdentifier: Any? = nil )
Parameters
fetchClauses
a series of
FetchClause
instances for fetching the object list. AcceptsWhere
,OrderBy
, andTweak
clauses.sourceIdentifier
an 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] = myObject
Important
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
section
the 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
nil
if out of bounds.Declaration
Swift
public func objects(safelyIn section: Int) -> [O]?
Parameters
section
the 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
section
the 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
nil
if out of bounds.Declaration
Swift
public func objects(safelyIn section: Int) -> [O]?
Parameters
section
the section index. Using an index outside the valid range will return
nil
.Return Value
all objects in the specified section