react-native-mmkv-storage
react-native-mmkv-storage copied to clipboard
Access storage from Android/iOS native
Wanted to check how can I access the default storage instance to be read/write from the native Android/iOS code.
This is required because I am working on a brownfield app and some of the values are updated/set in RN and need to be read in the native activities and vice versa.
I was debugging the flow and found that the mmapId used we when instantiate MMKV storage from RN, it is "mmkvIDStore". But when I try to fetch MMKV storage object, it does not seem to hold the values set in RN. Correct me if I am wrong.
Is the storage instance you are trying to access encrypted or not?
Is the storage instance you are trying to access encrypted or not?
Also mmkvIdStore
is an internal instance used by the library to hold some information regarding all instances that have been created. It will not hold values you set from RN.
Not encrypted.
Can you help me with the mmapId I should use to get the instance used by RN? Is mmapId same as instance id?
What is your initialization function for init the database in RN? Can you share it here??
In RN -
const storage = new MMKVStorage.Loader().initialize();
storage.setInt('tempInt', 100);
mmapID is same as instance id. You should use "default" as mmapID which should return the correct instance.
I did. Got the same instance with default. But got the incorrect value.
Writing this on Android -
MMKV instance = MMKV.mmkvWithID("default");
instance.containsKey("tempInt"); // returns true
instance.decodeInt("tempInt"); // return 0
I did. Got the same instance with default. But got the incorrect value.
Writing this on Android -
MMKV instance = MMKV.mmkvWithID("default"); instance.containsKey("tempInt"); // returns true instance.decodeInt("tempInt"); // return 0
That's because the value is stored as double.
I did. Got the same instance with default. But got the incorrect value.
Writing this on Android -
MMKV instance = MMKV.mmkvWithID("default"); instance.containsKey("tempInt"); // returns true instance.decodeInt("tempInt"); // return 0
That's because the value is stored as double.
If containsKey returns true, it means you are accessing the correct instance.
I did. Got the same instance with default. But got the incorrect value. Writing this on Android -
MMKV instance = MMKV.mmkvWithID("default"); instance.containsKey("tempInt"); // returns true instance.decodeInt("tempInt"); // return 0
That's because the value is stored as double.
How can I retrieve it on the native side?
Use decodeDouble
method of mmkv.
Use
decodeDouble
method of mmkv.
I am using 0.6.12 of the library and there seems to be no function like that. Am I missing something here?
@anirudhagarwal1 I meant on Native Side.
MMKV instance = MMKV.mmkvWithID("default");
instance.containsKey("tempInt");
instance.decodeDouble("tempInt");
@ammarahm-ed I was talking about the native side itself. There seems to be no method called decodeDouble.
Only method available are -

There is no method to decode string as well.
@anirudhagarwal1 Ah ok. The library implements only a subset of methods on Android and the decodeDouble methods is missing because it is not used. At the moment it won't be possible to do this unless these methods are added in .cpp file in JNI on android.
This will require an update to be implemented.
Yep. Got it. Any plans to get those methods added in the near future?
@anirudhagarwal1 I will add them in the next version
Awesome. Thanks @ammarahm-ed I would have loved to create a PR myself but have never worked on JNI myself. Any estimate on when the next version is coming out?
any update on this ?
@ammarahm-ed @anirudhagarwal1
@ammarahm-ed Does https://github.com/ammarahm-ed/react-native-mmkv-storage/issues/217#issuecomment-1037232765 still work? I'm having issues accessing MMKV on Activity/Java/AndroidStudio
@sidferreira That should work, how are you adding the dependency to your project? For #343 the way we tested used the same function set and worked properly:
MMKV.initialize(this)
val mmkv = MMKV.mmkvWithID("default")
Log.d("DEBUG_TAG", "Native mmkv contains key \"my_id\": ${mmkv?.containsKey("my_id")}")
@josephyanks I added that as a way to try to make it work... I dunno why it just shows that error messages on AndroidStudio...