Kotlin: Offline
Cache reads and queue writes while the device is offline, then sync in order; kept on disk by default with the Kotlin SDK.
Cache reads and queue writes while the device is offline, then sync in order; kept on disk by default. Every call runs as the client's signed-in user, so your project's policies and row-level security apply. Types and members are listed in source order; each one links to its HTTP endpoint where it maps to one.
#Types and members
OfflineStore
A small key/value store for the offline cache and write queue. The default (FileOfflineStore) keeps it in files in the app's private data folder, so it survives a relaunch; pass MemoryOfflineStore to keep it for this run only, or your own store (Room, EncryptedSharedPreferences, …) for anything else.
interface OfflineStoreOfflineStore.get
suspend fun get(key: String): String?OfflineStore.set
suspend fun set(key: String, value: String)OfflineStore.delete
suspend fun delete(key: String)OfflineStore.keys
Keys that start with prefix.
suspend fun keys(prefix: String): List<String>MemoryOfflineStore
Keeps the offline cache and queue for this run only: queued writes not yet synced are lost on relaunch.
class MemoryOfflineStore : OfflineStoreMemoryOfflineStore.get
override suspend fun get(key: String)MemoryOfflineStore.set
override suspend fun set(key: String, value: String)MemoryOfflineStore.delete
override suspend fun delete(key: String)MemoryOfflineStore.keys
override suspend fun keys(prefix: String)SovariaOffline
Offline-first for the data API: when a request fails for lack of network, writes are queued (oldest first) and select/sql reads fall back to the last cached result. On reconnect the queue is replayed one request at a time, in order; a write the server refuses (a rule, a precondition) is dropped and reported to onSyncError. Mirrors the TypeScript offline controller, scoped to this SQL-first client.
class SovariaOffline internal constructor(
private val client: Sovaria,
internal val store: OfflineStore,
namespace: String?,
private val onSyncError: ((SovariaException, Map<String, Any?>) -> Unit)?,
private val onSynced: ((Int) -> Unit)?,
)SovariaOffline.online
False after a request failed for lack of network; true after one reached the server.
val online: Boolean get()SovariaOffline.pendingWrites
Writes queued offline that haven't reached the server yet, oldest first.
suspend fun pendingWrites(): List<Map<String, Any?>>SovariaOffline.flush
Send queued writes now (also runs on reconnect and every 30 s while some are pending).
suspend fun flush(): IntReturns how many synced.
SovariaOffline.clear
Forget the cache and the queue (e.g.
suspend fun clear()on sign-out).
SovariaOffline.MAX_QUEUED
Writes waiting at once; past this a new offline write is refused with OFFLINE_QUEUE_FULL instead of growing the store without bound (SDK2-M14).
const val MAX_QUEUEDSovariaOffline.MAX_CACHED
Cached read results kept; the oldest are dropped past this (SDK2-M14).
const val MAX_CACHEDSovariaOffline.migrate
Copy the offline entries (sov: keys) of from into to, keeping any to already has.
suspend fun migrate(from: OfflineStore, to: OfflineStore, prefix: String = ""): IntUse it when an app that kept its offline queue in one store moves to another (for example from MemoryOfflineStore to the persistent default), before enableOffline(store = to). Returns how many entries were copied.
SovariaOffline.close
Stop the background sync timer.
fun close()FileOfflineStore
The offline store enableOffline() uses when none is given: files in the app's private data folder, so cached reads and queued writes survive a relaunch. One small file per key, written to a temporary file and moved into place atomically, so a crash mid-write never leaves a half-written entry.
If the folder can't be created or written, the store keeps working in memory for this run and isPersistent turns false, rather than failing the app's requests.
class FileOfflineStore(directory: File? = null) : OfflineStoreFileOfflineStore.directory
A property.
val directory: FileFileOfflineStore.isPersistent
False when the folder couldn't be written and this run keeps entries in memory only.
val isPersistent: Boolean get()FileOfflineStore.directoryOverride
Where every default store keeps its files; set it before enableOffline() to choose the folder (on Android, e.g.
@Volatile @JvmStatic var directoryOverride: File?File(context.filesDir, "sovaria-offline")).
FileOfflineStore.defaultDirectory
directoryOverride, else SOVARIA_OFFLINE_DIR, else the platform's app-data folder (Android: the app's files folder; macOS: Application Support; Windows: %APPDATA%; Linux: $XDG_DATA_HOME or ~/.local/share), each with sovaria/offline appended.
fun defaultDirectory(): FileFileOfflineStore.get
override suspend fun get(key: String): String?FileOfflineStore.set
override suspend fun set(key: String, value: String)FileOfflineStore.delete
override suspend fun delete(key: String)FileOfflineStore.keys
override suspend fun keys(prefix: String): List<String>