Opening Documents
Documents is the entry point. Open a document directly on the default store, or
open a named Collection first when a set of documents needs its own MMKV file.
The default store
Documents.document<T>(key) is the common case — it opens (get-or-open, not
"create anew") the document under key in the default store, with no config
required:
@Serializable
data class Note(val title: String = "", val body: String = "", val done: Boolean = false)
val note = Documents.document<Note>("note-1")
A configuration block is optional and, today, only exposes dispatcher — the
dispatcher used to collect flow()/stateFlow() (see
Reactivity):
val note = Documents.document<Note>("note-1") {
dispatcher = Dispatchers.Default // this is already the default
}
Collections — a separate MMKV file
Reach for Documents.collection(name) only when a set of documents needs its own
file — a wipe-on-logout cache, per-user scoping, or an encryption boundary:
val drafts = Documents.collection("drafts") {
dispatcher = Dispatchers.Default
}
val draft = drafts.document<Note>("draft-1")
See Collections & Testing for the single-process constraint that applies to every collection, including the default store.
The reserved key separator
Internally, every document is decomposed into one storage key per field, keyed
{document}::{field} (see Field
Decomposition). Because :: is reserved for that purpose, a document
key containing it is rejected with an IllegalArgumentException —
choose ordinary identifiers like "note-1" or "user".
No setup ceremony
MMKV initializes itself the first time it's needed — on Android via
androidx.startup, on iOS via an internal call using the in-process sandbox path. You
never call MMKV.initialize() and never pass a Context; the first
Documents.document(...) or Documents.collection(...) call is enough.
On Android, this relies on a <provider
android:name="androidx.startup.InitializationProvider"> entry merged into your app's
manifest. Leave it alone — removing or overriding that merged entry (for example with a
conflicting tools:node="remove") stops MMKV from initializing, and document access
will fail at runtime.