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 ConnectivityManager.NetworkCallback to detect when the network becomes reachable.

// Application.onCreate (recommended) or Activity.onCreate
client.offlineQueue.startMonitoring(applicationContext)

Requires ACCESS_NETWORK_STATE permission — already declared in the SDK’s manifest, so no extra setup needed in your app.

Enqueue a mutation when you detect the device is offline:

try {
posts.create(newPost)
} catch (e: BunBaseError.NetworkError) {
client.offlineQueue.enqueue("POST", "/api/v1/posts", body = newPost)
}
data class NewPost(val title: String, val body: String)
client.offlineQueue.enqueue("POST", "/api/v1/posts", body = NewPost("Hello", "World"))
val json = """{"title":"Hello","body":"World"}"""
client.offlineQueue.enqueue("POST", "/api/v1/posts", bodyJson = json)
client.offlineQueue.enqueue("DELETE", "/api/v1/posts/${post.id}")
client.offlineQueue.enqueue("PATCH", "/api/v1/posts/${post.id}", body = mapOf("title" to "Updated"))

Listen for per-item results after a flush:

client.offlineQueue.onFlushResult = { id, error ->
if (error != null) {
Log.e("BunBase", "[$id] failed: $error")
} else {
Log.d("BunBase", "[$id] replayed successfully")
}
}

Flush behaviour by response:

Server responseResult
2xxItem removed from queue, onFlushResult(id, null)
4xxItem removed (permanent failure), onFlushResult(id, error)
5xx / networkItem kept, onFlushResult(id, error) — will retry next flush
lifecycleScope.launch {
client.offlineQueue.flush()
}
val count = client.offlineQueue.pendingCount // Int
val pending = client.offlineQueue.items // List<QueuedMutation>

Discards all pending mutations without replaying them.

client.offlineQueue.clear()
client.offlineQueue.stopMonitoring()

The queue is persisted to SharedPreferences as JSON and survives process death. Auth tokens are refreshed automatically during replay — no extra setup needed.

class PostViewModel : ViewModel() {
fun createPost(title: String, body: String) {
viewModelScope.launch {
try {
client.collection("posts", Post::class.java)
.create(Post(title, body))
} catch (e: BunBaseError.NetworkError) {
// Queue the write — it will replay when online
client.offlineQueue.enqueue(
method = "POST",
path = "/api/v1/posts",
body = Post(title, body),
)
// Optimistically update local UI here
}
}
}
}