Skip to content

Offline Request Queue

Husnain Ali edited this page Sep 27, 2026 · 2 revisions

Offline Request Queue

Opt-in per endpoint. A request sent while offline is archived and the caller gets NetworkError.offlineQueued(id) immediately; the queue replays FIFO when connectivity returns.

config.offlineStore   = FileOfflineStore()          // persisted; survives launches
config.networkMonitor = PathNetworkMonitor()

struct SubmitOrder: Endpoint {
    typealias Response = Order
    var method: HTTPMethod { .post }
    var offlineBehavior: OfflineBehavior { .queue(expiresAfter: 3600) }
}
for await event in await client.offlineReplayEvents() {
    switch event {
    case .replayed(let id, let statusCode): ...
    case .failed(let id, let error): ...
    case .expired(let id): ...
    }
}

await client.replayOfflineQueue()   // also force a replay manually

A replayed request is sent exactly as it was archived: it keeps the Authorization header it had, with no token-refresh hop, so one that outlives its token replays as a 401 (delivered as .replayed with that status). There is also no idempotency guard, so only queue requests that are safe to send twice, or that the server de-duplicates.

Multipart bodies cannot be archived, so a multipart endpoint always fails fast instead of queueing.

A queued request is never dropped silently:

  • A request whose re-send keeps failing at the transport level is dropped after 10 attempts, and .failed is emitted each time.
  • A queued request that can no longer be rebuilt emits .failed before it is dropped.
  • FileOfflineStore logs write failures, moves a file that is not valid JSON aside to <file>.corrupt, and leaves a file it cannot read untouched. Pass logger: to route those lines.

Clone this wiki locally