mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-26 11:21:32 +01:00
Support for fetchOffset in ListPublisher, optimize slicing logic
This commit is contained in:
@@ -47,9 +47,13 @@ extension Internals {
|
|||||||
|
|
||||||
// MARK: Internal
|
// MARK: Internal
|
||||||
|
|
||||||
init(sections: [NSFetchedResultsSectionInfo]) {
|
init(sections: [NSFetchedResultsSectionInfo], fetchOffset: Int, fetchLimit: Int) {
|
||||||
|
|
||||||
self.structure = .init(sections: sections)
|
self.structure = .init(
|
||||||
|
sections: sections,
|
||||||
|
fetchOffset: Swift.max(0, fetchOffset),
|
||||||
|
fetchLimit: (fetchLimit > 0) ? fetchLimit : nil
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
var sections: [Section] {
|
var sections: [Section] {
|
||||||
@@ -192,17 +196,6 @@ extension Internals {
|
|||||||
self.structure.update(sectionIDs: identifiers)
|
self.structure.update(sectionIDs: identifiers)
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating func resize(limit: Int) {
|
|
||||||
|
|
||||||
let itemIdentifiers = self.itemIdentifiers
|
|
||||||
guard itemIdentifiers.endIndex > limit else {
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
self.structure.remove(itemIDs: itemIdentifiers.suffix(from: limit))
|
|
||||||
self.structure.removeAllEmptySections()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// MARK: Private
|
// MARK: Private
|
||||||
|
|
||||||
@@ -285,17 +278,45 @@ extension Internals {
|
|||||||
self.sections = []
|
self.sections = []
|
||||||
}
|
}
|
||||||
|
|
||||||
init(sections: [NSFetchedResultsSectionInfo]) {
|
init(sections: [NSFetchedResultsSectionInfo], fetchOffset: Int, fetchLimit: Int?) {
|
||||||
|
|
||||||
self.sections = sections.map {
|
let sliceItems: (_ array: [Any], _ offset: Int) -> Array<Any>.SubSequence
|
||||||
|
if let fetchLimit = fetchLimit {
|
||||||
|
|
||||||
Section(
|
var remainingCount = fetchLimit
|
||||||
differenceIdentifier: $0.name,
|
sliceItems = {
|
||||||
items: $0.objects?
|
|
||||||
.compactMap({ ($0 as? NSManagedObject)?.objectID })
|
let slice = $0[$1...].prefix(remainingCount)
|
||||||
.map({ Item(differenceIdentifier: $0) }) ?? []
|
remainingCount -= slice.count
|
||||||
|
return slice
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
sliceItems = { $0[$1...] }
|
||||||
|
}
|
||||||
|
var newSections: [Internals.DiffableDataSourceSnapshot.Section] = []
|
||||||
|
var ignoreCount = fetchOffset
|
||||||
|
for section in sections {
|
||||||
|
|
||||||
|
let objects = section.objects ?? []
|
||||||
|
guard objects.indices.contains(ignoreCount) else {
|
||||||
|
|
||||||
|
ignoreCount -= objects.count
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
let items = sliceItems(objects, ignoreCount)
|
||||||
|
.map({ Item(differenceIdentifier: ($0 as! NSManagedObject).objectID) })
|
||||||
|
ignoreCount = 0
|
||||||
|
guard !items.isEmpty else {
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newSections.append(
|
||||||
|
Section(differenceIdentifier: section.name, items: items)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
self.sections = newSections
|
||||||
}
|
}
|
||||||
|
|
||||||
var allSectionIDs: [String] {
|
var allSectionIDs: [String] {
|
||||||
|
|||||||
@@ -91,13 +91,10 @@ extension Internals {
|
|||||||
dynamic func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
|
dynamic func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
|
||||||
|
|
||||||
var snapshot = Internals.DiffableDataSourceSnapshot(
|
var snapshot = Internals.DiffableDataSourceSnapshot(
|
||||||
sections: controller.sections ?? []
|
sections: controller.sections ?? [],
|
||||||
|
fetchOffset: controller.fetchRequest.fetchOffset,
|
||||||
|
fetchLimit: controller.fetchRequest.fetchLimit
|
||||||
)
|
)
|
||||||
let fetchLimit = controller.fetchRequest.fetchLimit
|
|
||||||
if fetchLimit > 0 {
|
|
||||||
|
|
||||||
snapshot.resize(limit: fetchLimit)
|
|
||||||
}
|
|
||||||
snapshot.reloadSections(self.reloadedSectionIDs)
|
snapshot.reloadSections(self.reloadedSectionIDs)
|
||||||
snapshot.reloadItems(self.reloadedItemIDs)
|
snapshot.reloadItems(self.reloadedItemIDs)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user