ammarahm-ed / react-native-mmkv-storage

An ultra fast (0.0002s read/write), small & encrypted mobile key-value storage framework for React Native written in C++ using JSI
https://rnmmkv.now.sh
MIT License
1.56k stars 109 forks source link

Add more native access methods for android #343

Closed josephyanks closed 6 days ago

josephyanks commented 7 months ago

Adds getAllKeys and decodeString, decodeDouble, and encode methods for Int, Double, and String to android to allow reading more values from android native code.

Relates to #217

If you want to access MMKV before RN is initialized from the android native code, you will need to call load earlier.

Example of new method usage:

 // Load the library (if RN isn't loaded at this point)        
System.loadLibrary("rnmmkv");
MMKV.initialize(this)
val mmkv = MMKV.mmkvWithID("default")!!
val allKeys = mmkv.getAllKeys()
val keyValuePairs = allKeys.associateWith { mmkv.decodeString(it, "") }
Log.w("MyApplication", keyValuePairs.entries.joinToString(",") { "[${it.key}:${it.value}]"})

// Doubles
mmkv.encode("testDouble",60.6)
val doubleValue = mmkv.decodeDouble("testDouble")
Log.w("MyApplication", "doubleValue=$doubleValue")
vercel[bot] commented 7 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rnmmkv ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 19, 2024 5:45pm
josephyanks commented 7 months ago

@ammarahm-ed wondering what steps we can take to get this merged, otherwise we're going to probably have to patch our version locally.

Let me know if there's anything I can do to move this forward.

sidferreira commented 7 months ago

@josephyanks how to use that on iOS?

josephyanks commented 7 months ago

@josephyanks how to use that on iOS?

These are bindings specifically for android. For iOS (will verify at a later point) it seems like there isn't the need for an equivalent layer (due to no JNI bindings required) and I believe that you can just use the C++ MMKV libraries directly.

sidferreira commented 7 months ago

@josephyanks will give it another try...

AntonGolikov commented 7 months ago

@sidferreira hey. I don't know if you figured out the iOS part yet, but here's the example how you could do it. Just a little disclaimer, if you're using RN-MMKV from the box, this solution should work fine. But if not, you'll have to figure our all things related to path and mapID to initialize it properly.

Here's how could you access your RN MMKV storage from Swift:

import MMKV 

func getStorage() -> MMKV? {
    let paths = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)
    guard let first = paths.first else { return nil }
    let path = (first as NSString).appendingPathComponent("mmkv")
    MMKV.initialize(rootDir: path as String)
    return MMKV.init(mmapID: "default")
}

let storage = getStorage()
let keys: [Any] = storage?.allKeys()
let stringKeys: [String] = storage?.allKeys().compactMap { $0 as? String }
// example of accessing values of different kinds
let myString: String? = storage?.string(forKey: "my_key_string")
let myBool: Bool? = storage?.bool(forKey: "my_key_bool")
let myDouble: Double? = storage?.double(forKey: "my_key_double")
sidferreira commented 7 months ago

@AntonGolikov do you have a similar snippet for Android? I hard a hard time making it work...

josephyanks commented 7 months ago

@sidferreira

You should pull the latest changes (I had to fix a build issue), but the code from the PR description up top should work.

sidferreira commented 7 months ago

@sidferreira

You should pull the latest changes (I had to fix a build issue), but the code from the PR description up top should work.

oh, this works only within this PR?

josephyanks commented 5 months ago

@sidferreira You should pull the latest changes (I had to fix a build issue), but the code from the PR description up top should work.

oh, this works only within this PR?

From @AntonGolikov's post:

if you're using RN-MMKV from the box, this solution should work fine.

josephyanks commented 5 months ago

Pinging @ammarahm-ed to see if there's something we can do to get this merged in.

ammarahm-ed commented 6 days ago

Thanks for these much needed changes. Looks good!