Swift: Offline
Cache reads and queue writes while the device is offline, then sync in order; kept on disk by default with the Swift 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 Application Support, so it survives a relaunch; pass MemoryOfflineStore() to keep it for this run only, or your own store (e.g. encrypted) for anything else.
public protocol OfflineStore: AnyObject, SendableOfflineStore.get
func get(_ key: String) async -> String?OfflineStore.set
func set(_ key: String, _ value: String) asyncOfflineStore.delete
func delete(_ key: String) asyncOfflineStore.keys
Keys that start with prefix.
func keys(_ prefix: String) async -> [String]MemoryOfflineStore
Keeps the offline cache and queue for this run only: queued writes not yet synced are lost on relaunch.
public final class MemoryOfflineStore: OfflineStore, @unchecked SendableMemoryOfflineStore.MemoryOfflineStore.init
Create an instance.
public init()MemoryOfflineStore.get
public func get(_ key: String) async -> String?MemoryOfflineStore.set
public func set(_ key: String, _ value: String) asyncMemoryOfflineStore.delete
public func delete(_ key: String) asyncMemoryOfflineStore.keys
public func keys(_ prefix: String) async -> [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.
public final class SovariaOffline: @unchecked SendableSovariaOffline.maxQueued
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).
public static let maxQueuedSovariaOffline.maxCached
Cached read results kept; the oldest are dropped past this (SDK2-M14).
public static let maxCachedSovariaOffline.online
False after a request failed for lack of network; true after one reached the server.
public var online: BoolSovariaOffline.pendingWrites
Writes queued offline that haven't reached the server yet, oldest first.
public func pendingWrites() async -> [[String: Any]]SovariaOffline.flush
Send queued writes now (also runs on reconnect and every 30 s while some are pending).
public func flush() async throws -> IntReturns how many synced.
SovariaOffline.clear
Forget the cache and the queue (e.g.
public func clear() asyncon sign-out).
SovariaOffline.close
Stop the background sync timer.
public func close()FileOfflineStore
The offline store enableOffline() uses when none is given: files in the app's Application Support folder, so cached reads and queued writes survive a relaunch. One small file per key, written atomically (temp file + rename), so a crash mid-write never leaves a half-written entry. On Apple platforms the folder is excluded from iCloud backups and readable after the first unlock (background sync works with the device locked).
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.
public final class FileOfflineStore: OfflineStore, @unchecked SendableFileOfflineStore.directoryOverride
Where every default store keeps its files; set it before enableOffline() to choose the folder.
public static var directoryOverride: URL?FileOfflineStore.directory
A property.
public let directory: URLFileOfflineStore.FileOfflineStore.init
Create an instance.
public init(directory: URL? = nil)FileOfflineStore.isPersistent
False when the folder couldn't be written and this run keeps entries in memory only.
public var isPersistent: BoolFileOfflineStore.defaultDirectory
directoryOverride, else SOVARIA_OFFLINE_DIR, else Application Support (~/.local/share on Linux) + sovaria/offline.
public static func defaultDirectory() -> URLFileOfflineStore.get
public func get(_ key: String) async -> String?FileOfflineStore.set
public func set(_ key: String, _ value: String) asyncFileOfflineStore.delete
public func delete(_ key: String) asyncFileOfflineStore.keys
public func keys(_ prefix: String) async -> [String]SovariaOffline.migrate
Copy the offline entries (sov: keys) of from into to, keeping any to already has.
public static func migrate(from: OfflineStore, to: OfflineStore, prefix: String = "") async -> 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.