# KStore: A Kotlin Multiplatform Library for Saving and Restoring Objects to Disk

DevFeed: [KStore: A Kotlin Multiplatform Library for Saving and Restoring Objects to Disk](<https://devfeed.tech/articles/kstore-25913.md>)

Original publisher: [Read original article](<https://medium.com/@xxfast/kstore-1be37dd7bc22?source=rss-43bae76e8f81------2>)

Author: Isuru Rajapakse

Published: 2022-10-02T16:01:17Z

Content type: tutorial

Language: en

Sources: [Stories by Isuru Rajapakse on Medium](<https://devfeed.tech/sources/stories-by-isuru-rajapakse-on-medium.md>)

Topics: [Kotlin Multiplatform](<https://devfeed.tech/topics/kotlin-multiplatform.md>), [Library](<https://devfeed.tech/topics/library.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [JSON](<https://devfeed.tech/topics/json.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [CI/CD](<https://devfeed.tech/topics/cicd.md>), [Filesystems](<https://devfeed.tech/topics/filesystems.md>), [GitHub Actions](<https://devfeed.tech/topics/github-actions.md>), [Open Source](<https://devfeed.tech/topics/open-source.md>), [test-coverage](<https://devfeed.tech/topics/test-coverage.md>)

Tags: [ci-cd](<https://devfeed.tech/tags/ci-cd.md>), [code-coverage](<https://devfeed.tech/tags/code-coverage.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [dependencies](<https://devfeed.tech/tags/dependencies.md>), [github-actions](<https://devfeed.tech/tags/github-actions.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-multiplatform](<https://devfeed.tech/tags/kotlin-multiplatform.md>), [library](<https://devfeed.tech/tags/library.md>), [open-source](<https://devfeed.tech/tags/open-source.md>), [serialization](<https://devfeed.tech/tags/serialization.md>), [testing](<https://devfeed.tech/tags/testing.md>)

## AI overview

KStore is a small Kotlin Multiplatform library for saving and restoring serialized objects to disk. The article describes its coroutine-based API, storage operations, mutex-protected updates, and project tooling for testing, CI/CD, code coverage, and binary compatibility validation.

## Source excerpt

A tiny Kotlin multiplatform library to save and restore objects to and from the disk So I put out a new library, my first Multiplatform library. KStore is a tiny Kotlin multiplatform library that assists in saving and restoring objects to and from disk using kotlinx.coroutines, kotlinx.serializationand okio Why? A newer version of kotlinx.serialization was released and one thing caught my eye Integration with Okio's BufferedSource and BufferedSink Release 1.4.0-RC - Kotlin/kotlinx.serialization However, there was little to no documentation on how to use it. I scanned through the repository looking for tests but couldn't find any. I asked on slack Got nothing.. But I pulled it down and tried it out anyways. dependencies { implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-okio:1.4.0") } Tried out the most obvious way and wrote up a test case Json.encodeToBufferedSink(value, FILE_SYSTEM.sink(path).buffer()) No luck. Asked around on slack once again Not a minute went by, and our lord and saviour himself descended from heaven to help me out After using use, it worked 🙌. His divinely presence inspired me to write out a bunch more test cases and put this out there in under ~48hrs GitHub - xxfast/KStore: A tiny Kotlin multiplatform library that assists in saving and restoring objects to and from disk using kotlinx.coroutines, kotlinx.serialisation and okio API KStore is inspired by RxStore, and it provides a simple API. Most of these interface methods are suspended, and can only be invoked from a suspended context. SuspendedCreate a storeval store: KStore<Pet> = storeOf("path/to/file")Get valueval mylo: Pet? = store.get() Or observe for changes val pets: Flow<Pet?> = store.updatesSet valuestore.set(mylo)Update a valuestore.update { pet: Pet? -> pet?.copy(age = pet.age + 1) } Note: this maintains a single mutex lock transaction, unlike get() and a subsequent set() Delete/Reset valuestore.delete() You can also reset a value back to its default. store.reset()Sel