Shared KMP Persistence
One storage API in commonMain — the same document code runs unchanged on Android
and iOS, reading and writing through the same MMKV-backed store on both platforms.
// commonMain
@Serializable
data class OfflineQueueItem(
val id: String,
val endpoint: String,
val payloadJson: String,
val attempts: Int = 0,
)
class PendingRequestQueue {
private val doc = Documents.document<List<OfflineQueueItem>>("pending-requests")
fun enqueue(item: OfflineQueueItem) {
doc.update { current -> (current ?: emptyList()) + item }
}
fun observe(): Flow<List<OfflineQueueItem>> =
doc.flow().map { it ?: emptyList() }
}
PendingRequestQueue is written once, in commonMain, and used
identically from an Android ViewModel and an iOS view model — there is no
platform-specific persistence code to write or keep in sync, because the entire public API and
its logic live in commonMain. Only the underlying Storage implementation
differs per platform, and that's an internal detail the library owns. See
Storage SPI and
Platform Support for how each target is bound.