MMKV for Kotlin Multiplatform is a wrapper for MMKV using Kotlin API

Overview

MMKV for Kotlin Multiplatform

中文版本请参看这里

MMKV for Kotlin Multiplatform is a wrapper for MMKV using Kotlin API, developed by Ctrip Flight mobile team. Currently only KMM (Android/iOS) is supported, It may be gradually expanded to macOS in the future.

Tutorial

Installation Via Maven in Gradle

Kotlin Multiplatform Common (kts):

dependencies {     
    implementation("com.ctrip.flight.mmkv:mmkv-kotlin:1.1.0")
}

Current version based on Kotlin 1.6.21 and MMKV 1.2.13.

Pure Android platform (kts):

dependencies {     
    implementation("com.ctrip.flight.mmkv:mmkv-kotlin-android:1.1.0")
}

Initialization and Configure Root Path

You can initialize MMKV when app or process initialization. MMKV-Android initialization API depends on Context. So, Android's initialization API is different from iOS.

Android:

import com.ctrip.flight.mmkv.initialize

// In Android source set
fun initializeMMKV(context: Context) {
    val rootDir = initialize(context)
    Log.d("MMKV Path", rootDir)
}

iOS:

import com.ctrip.flight.mmkv.initialize

// In iOS source set
fun initializeMMKV(rootDir: String) {
    initialize(rootDir)
    Log.d("MMKV Path", rootDir)
}

You also could call MMKV-Android (Java) or MMKV-iOS (Objective-C) initialization API in your Android or iOS app project.

CRUD Operations

  • MMKV has a global instance, you can use it directly::
import com.ctrip.flight.mmkv.defaultMMKV

fun demo() {
    val kv = defaultMMKV()

    kv.set("Boolean", true)
    println("Boolean: ${kv.takeBoolean("Boolean")}")

    kv.set("Int", Int.MIN_VALUE)
    println("Int: ${kv.takeInt("Int")}")

    kv.set("Long", Long.MAX_VALUE)
    println("Long: ${kv.takeLong("Long")}")

    kv.set("Float", -3.14f)
    println("Float: ${kv.takeFloat("Float")}")

    kv.set("Double", Double.MIN_VALUE)
    println("Double: ${kv.takeDouble("Double")}")

    kv.set("String", "Hello from mmkv")
    println("String: ${kv.takeString("String")}")

    val bytes = byteArrayOf(
        'm'.code.toByte(), 
        'm'.code.toByte(), 
        'k'.code.toByte(), 
        'v'.code.toByte(),
    )
    kv.set("ByteArray", bytes)
    println("ByteArray: ${kv.takeByteArray("ByteArray")?.toString()}")
}
  • Deleting & Querying:
kv.removeValueForKey("Boolean")
println("Boolean: ${kv.takeBoolean("Boolean")}")

kv.removeValuesForKeys(listOf("Int", "Long"))
println("allKeys: ${kv.allKeys()}")

val hasBoolean = kv.containsKey("Boolean")
  • If different modules/logics need isolated storage, you can also create your own MMKV instance separately:
import com.ctrip.flight.mmkv.mmkvWithID
...
val kvWithMyId = mmkvWithID("MyID")
kvWithMyId.set("Boolean", true)
  • If multi-process accessing is needed, you can set MMKV.MULTI_PROCESS_MODE on MMKV initialization:
import com.ctrip.flight.mmkv.mmkvWithID
import com.ctrip.flight.mmkv.MMKVModel

...
val kvMultiProcess = mmkvWithID("InterProcessKV", MMKVModel.MULTI_PROCESS)
kvMultiProcess.set("Boolean", true)

Supported Types

  • Supported Kotlin types in Kotlin Multiplatform common source set:

    • Boolean、Int、Long、Float、Double、String、UInt、ULong、ByteArray、Set
  • The following types are additionally supported in the Android source set:

    • Any class that implements Parcelable
  • The following types are additionally supported in the iOS source set:

    • NSDate and any class that implements NSCoding protocol

Note

  • MMKV-Kotlin currently does not support migrating old data from SharedPreferences and NSUserDefaults.

  • MMKV-Kotlin is based on old Kotlin/Native memory management in iOS,We plan to migrate to Kotlin/Native's new memory management when it is released.

License

Distributed under the Apache License, Version 2.0.

See LICENSE for more information.

You might also like...
intera.kt is a Kotlin library for interacting with the Discord Interactions API through a gateway service or a REST API.

🗿 Overview ⚠️ WARNING: intera.kt is a work in progress. It is not yet ready for use. You may encounter bugs and other issues, but please report if yo

intera.kt is a Kotlin library for interacting with the Discord Interactions API through a gateway service or a REST API.

🗿 Overview ⚠️ WARNING: intera.kt is a work in progress. It is not yet ready for use. You may encounter bugs and other issues, but please report if yo

Postman-API-Fest-22 - Project for Postman API Fest 22
Postman-API-Fest-22 - Project for Postman API Fest 22

Project for Postman API Fest 22 Team Moon With only two members on the board, we

Kotlin-phoenix - A set of tools aimed to bridge Phoenix with the Kotlin Multiplatform world

Kotlin Phoenix This project is aimed to allow developers to work with Phoenix Ch

FlowExt is a Kotlin Multiplatform library, that provides many operators and extensions to Kotlin Coroutines Flow

FlowExt | Kotlinx Coroutines Flow Extensions | Kotlinx Coroutines Flow Extensions. Extensions to the Kotlin Flow library | kotlin-flow-extensions | Coroutines Flow Extensions | Kotlin Flow extensions | kotlin flow extensions | Flow extensions

TODO API Client Kata for Kotlin Developers. The main goal is to practice integration testing using MockWebServer
TODO API Client Kata for Kotlin Developers. The main goal is to practice integration testing using MockWebServer

KataTODOApiClient for Kotlin We are here to practice integration testsing using HTTP stubbing. We are going to use MockWebServer to simulate a HTTP se

Kotlin app that shows a list of characters using the api SWAPI
Kotlin app that shows a list of characters using the api SWAPI

StarWarsCharacters Kotlin app that shows a list of characters using the api SWAPI (https://swapi.dev) This is a clean architecture app example built w

API for a library using Kotlin, Spring-boot and covered by test

Library API This API is to create Books and be able to borrow from them I'm using: Spring Boot version 2.5.6 Kotlin 1.5.31 Java 11 I'm implementing us

🌱 A test implementation of a Minecraft server using RESTful API taking advantage of the interoperability between Kotlin and Java.

🌱 Norin A test implementation of a Minecraft server using RESTful API taking advantage of the interoperability between Kotlin and Java. This project

Releases(1.2.5-alpha01)
Owner
Ctrip, Inc.
Ctrip.com open source projects
Ctrip, Inc.
Asynchronous Spring Initializr API wrapper for Kotlin/JVM

initializr-kt Asynchronous Spring Initializr API wrapper for Kotlin/JVM. This library provides the simplest DSL for initializing Spring Boot projects

Mikhail Titov 2 May 8, 2022
Simple api wrapper for nekos.life

nekos-kt Simple api wrapper for nekos.life Installation Maven: <repositories> <repository> <id>jitpack.io</id> <url>https://jitpack.io</url>

cattyn 6 Oct 24, 2022
Mocking for Kotlin/Native and Kotlin Multiplatform using the Kotlin Symbol Processing API (KSP)

Mockative Mocking for Kotlin/Native and Kotlin Multiplatform using the Kotlin Symbol Processing API (KSP). Installation Mockative uses KSP to generate

Mockative 121 Dec 26, 2022
This Kotlin Multiplatform library is for accessing the TMDB API to get movie and TV show content. Using for Android, iOS, and JS projects.

Website | Forum | Documentation | TMDb 3 API Get movie and TV show content from TMDb in a fast and simple way. TMDb API This library gives access to T

Moviebase 37 Dec 29, 2022
Xoxo is a simple wrapper around org.w3c.dom to parse XML using nice Kotlin APIs

Xoxo ?? Xoxo is a simple wrapper around org.w3c.dom to parse XML using nice Kotlin APIs. No more NodeList, .item(), etc... just use .children, .filter

Martin Bonnin 51 Sep 1, 2022
Katbox - Kotlin wrapper for catbox.moe and litterbox.catbox.moe

katbox katbox is a Kotlin multiplatform wrapper written with ktor and coroutines

Oliver Berg 2 Oct 5, 2022
Extensive Redis Pub-Sub wrapper for lettuce written in Kotlin

aware Extensive annotation-based Redis Pub-Sub wrapper for lettuce written in Kotlin. Aware was written to be a replacement for the very dated Banana

Subham 6 Dec 28, 2022
Integration Testing Kotlin Multiplatform Kata for Kotlin Developers. The main goal is to practice integration testing using Ktor and Ktor Client Mock

This kata is a Kotlin multiplatform version of the kata KataTODOApiClientKotlin of Karumi. We are here to practice integration testing using HTTP stub

Jorge Sánchez Fernández 29 Oct 3, 2022
Multiplatform HTTP-Client implementation using Ktor

Kotlin Multiplatform HTTP-Client using Ktor Simple demonstration of a Ktor-based multiplatform HTTP-client in Kotlin Overview base submodule: common c

Jan Weidenhaupt 2 Oct 21, 2022