Skip to main content

Storage

sinew-storage / sinew_storage: local storage with the same error model as the network.

A corrupted file, a full disk or an unreadable value becomes a typed exception instead of a crash, and a reinstall starts with an empty secure store.

What it holds​

  • KeyValueStore: plain preferences, namespaced by name. Never tokens or personal data.
  • SecureStore: small secrets, encrypted with FieldCipher. A value that can't be decrypted reads as absent, and is deleted.
  • processStorageCall: the guard that makes storage failures typed.
  • pagedQuery: a local list that pages like a remote one.

Use the stores​

// sample: kotlin-lib/sample/docs/src/main/kotlin/com/srctool/sinew/docs/Storage.kt#stores
suspend fun remember(platform: PlatformContext, refreshToken: String) {
val prefs = KeyValueStore.create(platform, name = "orders")
prefs.setString("theme", "dark")
val theme = prefs.getString("theme") // null when missing or written as another type

val secrets = SecureStore.create(platform)
secrets.write("refreshToken", refreshToken)
println("theme: $theme, token stored: ${secrets.read("refreshToken") != null}")
}

Failures​

FailureBecomes
a corrupted file, an undecryptable valueLocalStorageCorruptionException
the device is out of spaceStorageFullException
any other storage errorStorageReadException or StorageWriteException

Good to know​

  • A value read as another type than it was written is absent, as is a missing one.
  • On sign-out, call clearUserData on the SecureStore.
  • A reinstall starts empty on every platform: the values go with the app, even where the key survives (the iOS Keychain).