react-native-mmkv-storage icon indicating copy to clipboard operation
react-native-mmkv-storage copied to clipboard

Access storage from Android/iOS native

Open anirudhagarwal1 opened this issue 3 years ago • 24 comments

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.

anirudhagarwal1 avatar Feb 12 '22 11:02 anirudhagarwal1

Is the storage instance you are trying to access encrypted or not?

ammarahm-ed avatar Feb 12 '22 12:02 ammarahm-ed

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.

ammarahm-ed avatar Feb 12 '22 12:02 ammarahm-ed

Not encrypted.

anirudhagarwal1 avatar Feb 12 '22 12:02 anirudhagarwal1

Can you help me with the mmapId I should use to get the instance used by RN? Is mmapId same as instance id?

anirudhagarwal1 avatar Feb 12 '22 12:02 anirudhagarwal1

What is your initialization function for init the database in RN? Can you share it here??

ammarahm-ed avatar Feb 12 '22 12:02 ammarahm-ed

In RN -

const storage = new MMKVStorage.Loader().initialize();
storage.setInt('tempInt', 100);

anirudhagarwal1 avatar Feb 12 '22 12:02 anirudhagarwal1

mmapID is same as instance id. You should use "default" as mmapID which should return the correct instance.

ammarahm-ed avatar Feb 12 '22 12:02 ammarahm-ed

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

anirudhagarwal1 avatar Feb 12 '22 12:02 anirudhagarwal1

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.

ammarahm-ed avatar Feb 12 '22 12:02 ammarahm-ed

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.

ammarahm-ed avatar Feb 12 '22 12:02 ammarahm-ed

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?

anirudhagarwal1 avatar Feb 12 '22 12:02 anirudhagarwal1

Use decodeDouble method of mmkv.

ammarahm-ed avatar Feb 12 '22 13:02 ammarahm-ed

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 avatar Feb 12 '22 13:02 anirudhagarwal1

@anirudhagarwal1 I meant on Native Side.

MMKV instance = MMKV.mmkvWithID("default");
instance.containsKey("tempInt");
instance.decodeDouble("tempInt"); 

ammarahm-ed avatar Feb 12 '22 13:02 ammarahm-ed

@ammarahm-ed I was talking about the native side itself. There seems to be no method called decodeDouble.

Only method available are -

Screenshot 2022-02-12 at 7 30 59 PM

There is no method to decode string as well.

anirudhagarwal1 avatar Feb 12 '22 14:02 anirudhagarwal1

@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.

ammarahm-ed avatar Feb 12 '22 14:02 ammarahm-ed

Yep. Got it. Any plans to get those methods added in the near future?

anirudhagarwal1 avatar Feb 12 '22 14:02 anirudhagarwal1

@anirudhagarwal1 I will add them in the next version

ammarahm-ed avatar Feb 12 '22 14:02 ammarahm-ed

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?

anirudhagarwal1 avatar Feb 12 '22 14:02 anirudhagarwal1

any update on this ?

arjun-anar avatar Jan 26 '23 12:01 arjun-anar

@ammarahm-ed @anirudhagarwal1

arjun-anar avatar Jan 26 '23 12:01 arjun-anar

@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

Screenshot 2024-01-15 at 2 12 16 PM

sidferreira avatar Jan 15 '24 22:01 sidferreira

@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 avatar Jan 16 '24 19:01 josephyanks

@josephyanks I added that as a way to try to make it work... I dunno why it just shows that error messages on AndroidStudio...

sidferreira avatar Jan 16 '24 20:01 sidferreira