-
Notifications
You must be signed in to change notification settings - Fork 1
Pagination and Batch
Husnain Ali edited this page Sep 27, 2026
·
2 revisions
struct ListUsers: PaginatedEndpoint {
typealias Response = [User]
var page = 1
var path: String { "/users" }
var queryParameters: QueryParameters? { ["_page": .int(page), "_limit": .int(20)] }
func items(from response: [User]) -> [User] { response }
func nextPage(after response: [User]) -> Self? {
guard !response.isEmpty else { return nil }
var next = self; next.page += 1; return next
}
}
for try await pageOfUsers in client.paginate(ListUsers()) { ... } // one array per page
let everyone = try await client.collectAll(ListUsers(), max: 500) // flattened, cappednextPage(after:) gives the endpoint full control of the scheme (page number, cursor, Link:
header). paginate honors task cancellation and caps at maxPages (default 1000). paginate ends
quietly at the cap, but collectAll throws instead, so a partial array is never mistaken for the
whole list; raise the cap with collectAll(_:max:maxPages:).
let (profile, feed) = try await client.zip(GetProfile(), GetFeed())
let items = await client.batch(ids.map { GetItem(id: $0) }) // [Result<Item, NetworkError>]SwiftNetworkKit is source-available proprietary software (not open source). See FAQ Licensing and Support. Repo: https://github.com/ihusnainalii/SwiftNetworkKit · Docs: https://swiftnetworkkit.vercel.app/
Getting started
Guides
- Defining Endpoints
- Authentication and Token Refresh
- Security and Certificate Pinning
- Retry Policy
- Interceptors
- Request Management
- Caching
- Offline Request Queue
- Uploads Downloads and Progress
- Pagination and Batch
- Combine and SwiftUI
- Error Handling
- Observability
- Testing and Mocking
- Best Practices and a Complete Example
- Platform Notes, Thread Safety and Performance
Reference