Handle paged API responses
masterWhen an API response is paginated, you have two ways to handle it:
- AsyncSequence: Use
provider.paged(request)to iterate through all pages automatically using afor try awaitloop. - Manual Pagination: Use
provider.request(_:isPagedResponse:)to check if a response has more pages, andprovider.request(request, pageAfter: firstPageResult)to fetch the next page.
// Option 1: Using AsyncSequence to get all pages
var allApps: [App] = []
for try await pagedResult in provider.paged(request) {
allApps.append(contentsOf: pagedResult.data)
}
// Option 2: Manual pagination
let firstPageResult = try await provider.request(request)
if provider.request(request, isPagedResponse: firstPageResult) {
if let nextPage = try await provider.request(request, pageAfter: firstPageResult) {
let secondPageApps = nextPage.data
}
}