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

app groups: value undefined until restart

Open coopmoney opened this issue 1 year ago • 4 comments

I'm using this to share data between a share extension and a react native app and noticing an issues that is pretty easily reproducible.

Observed behavior (when react native app is closed):

  • call set('key','value') on extension
  • open app
  • call get('key') , returns 'value' (works as expected).

Observed behavior (when app is backgrounded):

  • call set('key','value') on extension
  • open app
  • call get('key') , returns undefined (unexpected).
  • at this point, if I close the app, and then reopen it, a call to get('key') now returns 'value'

So the value is indeed being written correctly to the shared container, however the app can only read it after a restart, which leads me to believe that the initialization code is a requirement in order to get the right value.

coopmoney avatar Jan 16 '24 05:01 coopmoney

Did you add AppGroup to Info.plist? https://github.com/mrousavy/react-native-mmkv/blob/6fa83b5f30fdbf4f91f192d62d0cef6483106177/ios/MmkvModule.mm#L49-L57

mrousavy avatar Jan 16 '24 08:01 mrousavy

of course. If the app groups were not correctly set up, there would be no case where I can share values between the app and the extension. It indeed works, but the issue here is that, it is not readable by the 'other side' until that app is restarted. hope that makes sense..

I'm going to try modifying the library such that the initialization code re-runs when the app state goes background -> foreground. If this fixes the issue then it would confirm my suspicion that there is some dependency on that code running in order for the values to be updated on the other side.

coopmoney avatar Jan 16 '24 19:01 coopmoney

Okay so the MMKV instance gets created with AppGroups... idk

mrousavy avatar Jan 16 '24 19:01 mrousavy

I can confirm I started having issues with data being stale in my ios extension.

Have been using AppGroups with MultiProcess mode for 6+months without any issue.

Recently upgraded to Expo 50 & RN 0.73, not sure if that's linked, but now my extension often reads stale data, and sometimes logs InvalidProtocolBuffer truncatedMessage

There seems to be recent iOS multiprocess PRs on the MMKV repo but I tried pinning the MMKV version to the latest 1.3.2 and no luck

https://github.com/Tencent/MMKV/pull/1222

EDIT : looking more closely, it seems that

  • the error from the PR above fixes an issue that was introduced in 1.3.2. PR has been merged 3 weeks ago but no new version has been released
  • v1.3.1 from releases https://github.com/Tencent/MMKV/releases/tag/v1.3.1 says it was fixing "a critical bug that might cause multi-process MMKV corrupt" introduced in v1.2.16

So right now iOS devs might want to wait for the next release or pin the MMKV iOS version to 1.3.1. I guess my issues are not due to Expo 50 / RN upgrade but to the fact that I upgraded many libs during that big upgrade and might have upgraded MMKV at the same time.

nmalzieu avatar Jan 22 '24 22:01 nmalzieu