How to make pagination with publishList? #409

Closed
opened 2025-12-29 15:31:08 +01:00 by adam · 1 comment
Owner

Originally created by @woodyjhonson on GitHub (Apr 21, 2023).

Hello everyone, I'm dealing with observers and I just can't figure out how to make pagination? Below is a sample code, thanks in advance for your responses.

https://github.com/woodyjhonson/corestore_todolist

`

 private var todosFetchOffset: Int = 0

private lazy var todoPublisher = CoreStoreDefaults.dataStack.publishList(
    From<Todo>()
        .orderBy(.descending(\.$updatedAt))
        .tweak { [weak self] in
            $0.fetchOffset = self?.todosFetchOffset ?? 0
            $0.fetchLimit = 5
        }
)
override func viewDidLoad() {
    dataSource.apply(todoPublisher.snapshot)
    todosFetchOffset = todoPublisher.snapshot.numberOfItems

    todoPublisher.addObserver(self) { [weak self] listPublisher in
        guard let self else { return }
        let snapshot: ListSnapshot<Todo> = listPublisher.snapshot
        self.todosFetchOffset = listPublisher.snapshot.numberOfItems
        self.dataSource.apply(snapshot)
    }
}

`

Originally created by @woodyjhonson on GitHub (Apr 21, 2023). Hello everyone, I'm dealing with observers and I just can't figure out how to make pagination? Below is a sample code, thanks in advance for your responses. [https://github.com/woodyjhonson/corestore_todolist](https://github.com/woodyjhonson/corestore_todolist) ` private var todosFetchOffset: Int = 0 private lazy var todoPublisher = CoreStoreDefaults.dataStack.publishList( From<Todo>() .orderBy(.descending(\.$updatedAt)) .tweak { [weak self] in $0.fetchOffset = self?.todosFetchOffset ?? 0 $0.fetchLimit = 5 } ) override func viewDidLoad() { dataSource.apply(todoPublisher.snapshot) todosFetchOffset = todoPublisher.snapshot.numberOfItems todoPublisher.addObserver(self) { [weak self] listPublisher in guard let self else { return } let snapshot: ListSnapshot<Todo> = listPublisher.snapshot self.todosFetchOffset = listPublisher.snapshot.numberOfItems self.dataSource.apply(snapshot) } } `
adam closed this issue 2025-12-29 15:31:09 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Apr 24, 2023):

Unfortunately, NSFetchedResultsController (which is what ListPublishers run on internally) does not honor the fetchLimit and fetchOffset of the fetch request. You can try this again if it works on recent iOS versions, but historically it is known to be ignored.

In cases like yours, we just fetch the whole list and manage the indexes/counts and applying them to ListPublisher.snapshot

@JohnEstropia commented on GitHub (Apr 24, 2023): Unfortunately, `NSFetchedResultsController` (which is what `ListPublisher`s run on internally) does not honor the `fetchLimit` and `fetchOffset` of the fetch request. You can try this again if it works on recent iOS versions, but historically it is known to be ignored. In cases like yours, we just fetch the whole list and manage the indexes/counts and applying them to `ListPublisher.snapshot`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#409