mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-12 04:10:36 +01:00
ListPublisher publishList with fetchOffset returns incomplete results #442
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @KodaKoder on GitHub (Sep 30, 2025).
Description
When using
publishList()with a query that includesfetchOffsetvia.tweak { fr in fr.fetchOffset = N }, the resulting snapshot contains fewer items than expected. ThefetchOffsetparameter is being applied twice: once by Core Data'sNSFetchedResultsControllerand again during snapshot construction, causing items to be incorrectly skipped.Environment
Steps to Reproduce
Expected Behavior
When
fetchOffset = 5andfetchLimit = 10, the publisher should return items 5-14 (10 items total).Actual Behavior
The publisher returns only items 10-14 (5 items total). The first 5 items from the Core Data fetch are skipped during snapshot creation.
Debug Evidence
Using LLDB breakpoint in
controllerDidChangeContent:This proves:
NSFetchedResultsControllercorrectly fetched 10 objects (items 5-14)Comparison with
fetchAllThe bug does not occur with
fetchAll():Root Cause
In
Internals.FetchedDiffableDataSourceSnapshotDelegate.swift, thecontrollerDidChangeContentmethod passesfetchOffsetto the snapshot initializer:The problem:
controller.sectionsalready contains pre-filtered objects (items 5-14) becauseNSFetchedResultsControllerappliedfetchOffset. The snapshot initializer then appliesfetchOffsetagain to these already-offset results, causing items 5-9 to be skipped.Flow of the bug:
fetchRequest.fetchOffset = 5, fetchLimit = 10NSFetchedResultsControllerfetches items 5-14 from Core Data (10 items) ✓controller.sectionscontains objects:[item5, item6, ..., item14]fetchOffset: 5and skips another 5 items[item10, ..., item14](only 5 items) ❌Proposed Fix
Change to always pass
fetchOffset: 0:The sections passed from
NSFetchedResultsControllerare already offset, so the snapshot initializer should not apply any additional offset.Test Case
To verify the fix works:
Impact
This bug breaks pagination implementations that rely on
fetchOffsetwithpublishList(). Developers using sliding window pagination or infinite scroll will see:Workaround
Until fixed, avoid using
fetchOffsetwithpublishList():fetchAll()instead ofpublishList()for paginated queriesAdditional Notes
The
BackingStructure.initmethod (lines 402-452 inInternals.DiffableDataSourceSnapshot.swift) correctly handlesfetchOffsetfor its intended use case (un-offset sections). The bug is specifically in howcontrollerDidChangeContentcalls this initializer with already-offset sections.