KinesteX

Pagination

For large result sets, use pagination with the lastDocId parameter to fetch subsequent pages.


How it works:

1. First request: Omit lastDocId to get the first page

2. Store the ID: Save the lastDocId from the response

3. Next request: Pass the saved ID as lastDocId to get the next page

4. Repeat: Continue until no more results are returned

Paginated Fetching
1// Fetch all workouts with pagination
2func fetchAllWorkouts() async throws -> [WorkoutModel] {
3    var allWorkouts: [WorkoutModel] = []
4    var lastDocId: String? = nil
5
6    repeat {
7        let result = await kinestex.fetchWorkouts(
8            category: "Fitness",
9            limit: 10,
10            lastDocId: lastDocId
11        )
12
13        switch result {
14        case .success(let response):
15            allWorkouts.append(contentsOf: response.workouts)
16            lastDocId = response.lastDocId
17
18            // If lastDocId is empty or nil, we've reached the end
19            if lastDocId?.isEmpty ?? true {
20                lastDocId = nil
21            }
22
23            print("Fetched page with \(response.workouts.count) workouts")
24
25        case .failure(let error):
26            throw error
27        }
28    } while lastDocId != nil
29
30    print("Total workouts fetched: \(allWorkouts.count)")
31    return allWorkouts
32}