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

How to use react-native-mmkv v3 with custom native code

Open roni-castro-shipt opened this issue 10 months ago • 10 comments

Description

I currently use MMKV for obtaining an anonymous ID from storage on both Android and iOS in a React Native app (version 0.75.5). The code snippet below initializes MMKV and retrieves a string value:

#import <MMKV/MMKV.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   [MMKV initializeMMKV:nil];
   MMKV *mmkv = [MMKV defaultMMKV];
   NSString *userInfo = [mmkv getStringForKey:storageKey];
   // do some stuff with that userInfo, and this works as expected when using the Xcode debugger
  ...
}
  • I've set ENV['RCT_NEW_ARCH_ENABLED'] = '1'
  • I'm on React Native version 0.75.5.
  • I don't have use_frameworks in my ios/Podfile.

After updating MMKV to version from 2.11.0 to 3.2.0, I encountered an error indicating that #import <MMKV/MMKV.h> could not be found, as it seems the native peer dependency for MMKV was removed in this latest version.

Image

To fix this, I tried manually adding the MMKV pod to my Podfile like so:

pod 'MMKV', "1.2.13"

Question:

For this version of the library (MMKV 3.2.0), why do I need to manually add the MMKV pod instead of relying on autolinking?

roni-castro-shipt avatar Feb 17 '25 19:02 roni-castro-shipt

Guten Tag, Hans here. 🍻

Thank you for your detailed issue report! It looks like you are experiencing some trouble with MMKV after your update.

First, regarding your question about manually adding the MMKV pod: in version 3.2.0, some changes were made and autolinking may not handle all cases perfectly, especially when it comes to native code. It's important to ensure that all required dependencies are properly configured.

To help us understand your crash, could you please provide more detailed logs? Specifically, the crash logs from Xcode would be very helpful. You can find them in the "Report Navigator" on the left sidebar of Xcode, or run your app through the terminal to get the logs there.

If you need help gathering those logs, just let me know!

In ze meantime, if you're finding this project helpful, consider supporting it through sponsorship. It helps keep ze project maintained and could lead to faster resolutions!

Note: If you think I made a mistake, please ping @mrousavy to take a look.

maintenance-hans[bot] avatar Feb 17 '25 19:02 maintenance-hans[bot]

same issue for me when upgrading from 2.12.2 to 3.2.0, . Adding that back to pod file, it seem to show previous version installed. might be a better way.

Image

quangcao-offerup avatar Feb 25 '25 01:02 quangcao-offerup

Hey @mrousavy do you think that using the newest version from mmkv 2.1.0 (Supports using MMKV directly in C++ code.) would be possible to share the lib between native and react native?

https://github.com/Tencent/MMKV/releases/tag/v2.1.0

If so, can we talk about sponsoring your work to do so?

Thanks

gfrancischini avatar Mar 15 '25 02:03 gfrancischini

hi - i'd appreciate a sponsorship, thank you! with that i can take a look soon

mrousavy avatar Mar 15 '25 02:03 mrousavy

@mrousavy When integrating MMKV natively on iOS, the app crashes whenever a value is entered while using react-native-mmkv 3.2.0 and following this doc

I tried installing different versions of the native MMKV library (1.3.9, 1.3.13, 2.0.0, and 2.1.0), but none of them resolved the issue. The app consistently crashes with the error shown in the video below.

https://github.com/user-attachments/assets/876ace5c-e3ca-4351-ae4e-1039577e999c

iOS crash in line bool isStoredOnStack() const { return (type == MMBufferType_Small); } in Pods/MMKVCore/MMBuffer com.facebook.react.runtime.JavaScript (17): EXC_BAD_ACCESS (code=1, address=0x0) A bad access to memory terminated the process.

On Android, everything works fine with MMKV 2.0.0 (2.1.0 is not supported and generates a build error)

Any insights on how to fix this crash for iOS? Thanks!

Reproducible repo: https://github.com/roni-castro-shipt/react-native-mmkv-crash

roni-castro-shipt avatar Mar 25 '25 15:03 roni-castro-shipt

I created a WIP PR for this in https://github.com/mrousavy/react-native-mmkv/pull/828, but after 2 hours I couldn't manage to build it yet. Linker error on iOS, and on Android it can't find the prefab.

mrousavy avatar Apr 08 '25 10:04 mrousavy

I am just in the process of updating a rather big project from react-native 0.73.8 to 0.78.2. RCT_NEW_ARCH_ENABLED=1is enabled and I am using react-native-mmkv version 3.2.0. I removed all native usages of mmkv, but still when executing the setfunction in Typescript I get the native Error com.facebook.react.runtime.JavaScript (9): EXC_BAD_ACCESS (code=1, address=0x0) in isStoredOnStack in MMBuffer.h.

idoodler avatar May 15 '25 13:05 idoodler

I am just in the process of updating a rather big project from react-native 0.73.8 to 0.78.2. RCT_NEW_ARCH_ENABLED=1is enabled and I am using react-native-mmkv version 3.2.0. I removed all native usages of mmkv, but still when executing the setfunction in Typescript I get the native Error com.facebook.react.runtime.JavaScript (9): EXC_BAD_ACCESS (code=1, address=0x0) in isStoredOnStack in MMBuffer.h.

The same problem.

Yurii-Lutsyk avatar May 26 '25 08:05 Yurii-Lutsyk

@roni-castro-shipt @idoodler Guys, please help!!! Did you manage to fix this and how?

Yurii-Lutsyk avatar May 26 '25 11:05 Yurii-Lutsyk

@roni-castro-shipt @idoodler Guys, please help!!! Did you manage to fix this and how?

I migrated to react-native-mmkv-storage, this works with iOS new Arch, we currently have different build issues on Android with New Arch, so thats not confirmed yet.

Its also noteworthy that you would need to migrate data, or just live with loosing data as react-native-mmkv-storage saves all files in an mmkv directory, also the AppGroup Identifier in the info.plist is different if you use it.

idoodler avatar May 26 '25 14:05 idoodler

Is there any workaround for this problem?

mathiasinthegit avatar Jul 29 '25 18:07 mathiasinthegit

Yes there is a temporary fix for this. If you are using 2.12.2.

amitmundraz08 avatar Jul 31 '25 14:07 amitmundraz08

Using the config below for "react-native-mmkv": "3.3.1", I still see a crash on iOS with the error of the video. Android works fine, as long as it's applied reactNativeArchitectures=arm64-v8a,x86_64 (not documented anywhere for v3)

iOS

Podfile

pod 'MMKV', '2.2.3' # just adding this dependency without any custom native code is enough to crash the app

RN 0.80

https://github.com/user-attachments/assets/1038055c-4297-4f66-bb37-071bc1d80e42

roni-castro-shipt avatar Sep 16 '25 21:09 roni-castro-shipt

To use custom native mmkv code with the new arch, it needs to use v4 (tested with 4.0.0-beta.9); v3 does not work for iOS when including the native MMKV pods inside the Podfile.

Example of the app working with v4 and native code I included here:

https://github.com/roni-castro-shipt/react-native-mmkv-crash

Main points for iOS:

It needs

  pod 'MMKV', :modular_headers => true
  pod 'MMKVCore', :modular_headers => true

roni-castro-shipt avatar Sep 16 '25 22:09 roni-castro-shipt

Yes V3 builds MMKV from scratch, so you cannot use MMKV as a Pod in native code.

V4 fixes this by using MMKV from the Pod.

mrousavy avatar Sep 17 '25 10:09 mrousavy