12.3 · Pagination, caching and conditional requests, and authentication refresh
A client is more than data(from:). Three problems decide whether it behaves well in production: getting all the data, not downloading it twice, and staying logged in without a stampede.
Mental model. Treat every response as data plus a policy: how to ask for the next page, how long it stays valid, and what to do when credentials expire.
Data and caching
Pagination with Codable, and HTTP caching with ETags.
- How do you decode and paginate an API response, and which pagination style is safest?
Model each response with Decodable types and decode in one place (JSONDecoder, with a configured key and date strategy as covered in the Foundation topic). A… - How does HTTP caching work with Cache-Control, ETag and conditional requests, and how does URLCache fit in?
HTTP caching is driven by response headers. Cache-Control tells the client how long a response is fresh (max-age=60) and whether it may be stored at all…
Authentication
Tokens, refresh races and 401 loops.
- How do you handle access-token expiry, refresh races and 401 loops?
A typical API issues a short-lived access token and a longer-lived refresh token. The client attaches the access token to each request (Authorization: Bearer…
More in Networking
- 12.1 · Sessions, configurations, tasks, delegates and background transfers
- 12.2 · HTTP versions, TLS, App Transport Security and certificate pinning
- 12.4 · WebSockets, multipart and large transfers, and network path monitoring
- 12.5 · Retries, backoff, idempotency and cancellation
- 12.6 · REST, GraphQL and gRPC, and building a client platform
- 12.7 · Image loading pipelines: download, decode, downsample, cache
- 12.8 · Building URLs, reading headers, classifying errors and timeouts
- 12.9 · Single-flight token refresh, streaming lines, multipart bodies and request signing
- 12.10 · Network.framework, background sessions, delegates versus async, diagnosing and measuring