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

Ensure values in RN and Native are in sync

Open samr11th opened this issue 2 years ago • 2 comments

Hello,

We are using this library in a humongous mobile app of ours since the speed and performance of MMKV is something we value. We do have some features which are in native, which integrate with our React Native application pretty well.

While moving our storage to MMKV, we encountered issues with syncing of values. To illustrate this issue better, consider the following table:

  • We have a function which gets the value, increments it, and sets it into MMKV
  • We do the same operation in RN and on Native side
  • Native always gets the correct value, but the RN value is overridden by Native MMKV
Native RN Step
1 - Set in native
- 1 Access in RN
- 2 Access, increment set in RN
- 3 Access, increment set in RN
- 4 Access, increment set in RN
- 5 Access, increment set in RN
- 6 Access, increment set in RN
- 7 Access, increment set in RN
- 8 Access, increment set in RN
2 - Access, increment and set in Native
3 - Access, increment and set in Native
4 - Access, increment and set in Native
- 4 Access in RN

This issue is a major concern since some of our features on either side could also tokens and other data into storage, but the one in native is always regarded as the source of truth even if we update it to a newer value in RN.

We have also tried:

  • Multiprocess on Android -> This works fine but not something we intend to turn on
  • Multiprocess on iOS -> Doesn't work. Same issue occurs

samr11th avatar Jul 07 '23 11:07 samr11th

Hey! I think what you're seeing is because RN MMKV is not running in multi-process mode. You need to set your native MMKV instance to multi-process, and do the same for react-native-mmkv as well (via a patch, maybe I can also expose that prop)

mrousavy avatar Jul 07 '23 12:07 mrousavy

Hey @mrousavy! I think this feature was added for iOS in v2.10.0. But this version requires react-native@^0.71

Unfortunately, we are currently restricted from upgrading so we are on [email protected] and [email protected]. But we did manage to patch both Native MMKV and React Native MMKV to use multi-process mode.

  • Android: everything worked fine after we turned on multi-process mode
  • iOS: even with multi-process on, we faced the same issue

I will see if we can extract what we have done to a new repo and share, but please bear with me on that it will take a bit of time.

samr11th avatar Jul 07 '23 12:07 samr11th

Hey - I think this has been fixed in V3 beta.

mrousavy avatar Jul 22 '24 15:07 mrousavy