Skip to content
BunBase BunBase BunBase Docs Alpha v0.1.0

Offline Queue

The offline queue buffers mutating requests (create, update, delete) when the device is offline and replays them in order when connectivity is restored.

Reads (list, get) are not queued — they need fresh data from the server.

Enable auto-flush on app launch. The queue uses NWPathMonitor to detect when the network becomes reachable.

// AppDelegate / App.swift
BunBaseClient.shared.offlineQueue.startMonitoring()

Enqueue a mutation when you detect the device is offline (or optimistically always, letting the SDK handle ordering):

// Explicit: enqueue when offline
do {
let post = try await posts.create(newPost)
} catch BunBaseError.networkError {
client.offlineQueue.enqueue(
method: "POST",
path: "/api/v1/posts",
body: newPost // Encodable — serialised to JSON automatically
)
}
let bodyData = try JSONEncoder().encode(newPost)
client.offlineQueue.enqueue(method: "POST", path: "/api/v1/posts", bodyData: bodyData)
client.offlineQueue.enqueue(method: "DELETE", path: "/api/v1/posts/\(post.id)")
client.offlineQueue.enqueue(method: "PATCH", path: "/api/v1/posts/\(post.id)", body: patch)

Listen for per-item results after a flush:

client.offlineQueue.onFlushResult = { id, error in
if let error {
print("[\(id)] failed: \(error)")
} else {
print("[\(id)] replayed successfully")
}
}

Flush behaviour by response:

Server responseResult
2xxItem removed from queue, onFlushResult(id, nil)
4xxItem removed (permanent failure), onFlushResult(id, error)
5xx / networkItem kept, onFlushResult(id, error) — will retry next flush
await client.offlineQueue.flush()
print(client.offlineQueue.pendingCount) // number of items waiting
print(client.offlineQueue.items) // [QueuedMutation] — FIFO order

Discards all pending mutations without replaying them. Useful if the user logs out or you decide the queued operations are no longer relevant.

client.offlineQueue.clear()

The queue is persisted to UserDefaults as JSON and survives app restarts. Tokens are refreshed automatically during flush — no extra setup needed.

client.offlineQueue.stopMonitoring()