Documents

What's new in v0.2.1

Version 0.2.1 fixes a crash on Android devices that run as a 32-bit process. In v0.1.0 and v0.2.0, those devices crashed at launch, whether or not the app opened a document.

  • No more launch crash. The app now starts on a 32-bit process. Documents.document(...) and Documents.collection(...) throw the new UnsupportedArchitectureException instead.
  • 32-bit is not supported. MMKV 2.x requires a 64-bit process. A separate legacy artifact is being considered, but nothing is decided.
  • Nothing to change in your code. The only API change is the new exception class.

See Platform Support for details.

What's new in v0.2.0

Version 0.2.0 makes it safe to open the same document in more than one place. In v0.1.0, a write was only seen by the instance that made it, so every part of an app had to share a single instance. Sharing a single instance is no longer required, but it is still the recommended approach when it is practical.

  • Instances stay in sync. A write through one instance now reaches flow(), stateFlow(), and fieldFlow() on every other instance of the same document. This holds whether the instances come from dependency injection, from repeated Documents.document(...) calls, or from separate Documents.collection(name) calls.
  • Updates are not lost. If two instances of the same document call update at the same time, both changes are applied one after the other.
  • Clearer error messages. Each cause of a DocumentDecodingException now has its own message that says what went wrong and how to fix it.
  • Nothing to change in your code. The public API is the same as in v0.1.0.

Opening Documents explains how to open the same document more than once. The Reactivity Model explains how the instances stay in sync.

Overview

Documents gives you typed, reactive documents on top of MMKV. You define a data class, treat it as a document, and get typed reads, copy-style updates, and Flow reactivity. It is fast, and it is pleasant to write.

@Serializable
data class Note(val title: String = "", val body: String = "", val done: Boolean = false)

val note = Documents.document<Note>("note-1") // one call, you have a document

note.set(Note(title = "Pick up milk", body = "2%, not whole"))
note.update(Note::done, true) // one field, no read
note.update { current -> // several fields, one atomic write
    current.copy(
        title = "Pick up oat milk",
        body = "The barista-approved kind",
    )
}
note.flow().collect { editor.render(it) } // the editor reacts to every write

Built for elegance

MMKV is extremely fast. It is memory-mapped and about as quick as anything on the platform. Out of the box, though, it only gives you raw keys and primitive values. Every typed object means writing your own serialization and key management again. The typed and reactive alternatives give up that speed in exchange for comfort, so you usually have to choose between fast and pleasant to use.

Documents does not make you choose. It puts a clean, idiomatic Kotlin API directly on top of MMKV: typed documents, property delegates, a copy()-style update, and Flow reactivity. The cost is a little CPU for serialization and never any extra I/O. Clean code on top, MMKV all the way down.

The problem

Android and Kotlin Multiplatform apps often need to store one record of structured data, such as a logged-in user, a settings object, a session, or a draft. None of the usual tools fit this well:

No library offered a document-oriented, typed, reactive API with MMKV's performance underneath. Documents is that library.

What it gives you

Single-process only

Every store is single-process. This includes the default store, every named collection, and the in-memory test backend. Concurrent access to the same store from more than one OS process is not supported and can corrupt it.

On Android, a Service runs in your app's own process unless its manifest entry sets android:process, so an ordinary service is safe. A component with android:process, or an iOS app extension such as a widget or a share extension, runs in a separate process. Documents does not set up MMKV there, and if two processes write the same store, updates can be silently lost.

This is a firm limit, not an edge case. If your app needs to share a store across processes, this library is not the right fit for that store today. Either design around a single owning process for each store, or keep the multi-process data out of Documents.

Built by

Where to next