Skip to content

Data Loss Migrating from V2 -> V3 with app group set on iOS #812

Closed

Description

@JacquesLeupin

When migrating from V2 -> V3 in an Expo app on iOS, application data stored through react-native-mmkv is lost. The data loss occurs as part of an upgrade we're attempting from old arch -> new arch, react native version 0.74.5 -> 0.76.7, and Expo SDK 51 -> Expo SDK 52. Attempting react-native-mmkv upgrade from version 2.12.2 to version 3.2.0

Our app contains an AppGroup for an iOS share extension.

We have a simple file which initializes the mmkv store without any configuration:

import { MMKV } from 'react-native-mmkv';

export const mmkvStorage = new MMKV();

I've tried many times to alter the mmkv config while upgrading to not lose the data without success (altering the config when initializing, maintaining a stable MMKV version/submodule from a forked version of the package, etc). Nothing's worked thus far.

Here are the native logs when react-native-mmkv initializes as version 2.12.2, part of Expo 51, RN 0.74.5:

Installing global.mmkvCreateNewInstance...
[I] <MMKV.cpp:167::initialize> version v2.0.2, page size 16384, arch arm64-v8a
[I] <MMKV.cpp:219::initializeMMKV> Apple Device: 3, version: 17, mlock enabled: 0
[I] <MMKV.cpp:228::initializeMMKV> root dir: /var/mobile/Containers/Data/Application/D6E26C10-0D3C-4D4D-862B-D7284BD0B57C/Documents/mmkv
[I] <libMMKV.mm:132::+[MMKV initializeMMKV:groupDir:logLevel:]> groupDir: /private/var/mobile/Containers/Shared/AppGroup/846A9DAC-AB6F-40F2-82D4-A8A3C3A0999E/mmkv
Installed global.mmkvCreateNewInstance!
[D] <libMMKV.mm:927::+[MMKV mmapKeyWithMMapID:rootPath:]> mmapKey: 1df96a782b76e1e46f48a86659296c95
[I] <MMKV.cpp:255::mmkvWithID> prepare to load mmkv.default (id 1df96a782b76e1e46f48a86659296c95) from rootPath /private/var/mobile/Containers/Shared/AppGroup/846A9DAC-AB6F-40F2-82D4-A8A3C3A0999E/mmkv
[I] <MemoryFile.cpp:101::open> open fd[0x10], flag 7, /private/var/mobile/Containers/Shared/AppGroup/846A9DAC-AB6F-40F2-82D4-A8A3C3A0999E/mmkv/mmkv.default
[I] <MemoryFile.cpp:215::mmap> mmap to address [0x103718000], oldPtr [0x0], [/private/var/mobile/Containers/Shared/AppGroup/846A9DAC-AB6F-40F2-82D4-A8A3C3A0999E/mmkv/mmkv.default]
[I] <MemoryFile_OSX.cpp:35::tryResetFileProtection> protection on [/private/var/mobile/Containers/Shared/AppGroup/846A9DAC-AB6F-40F2-82D4-A8A3C3A0999E/mmkv/mmkv.default] is NSFileProtectionCompleteUntilFirstUserAuthentication
[I] <MemoryFile.cpp:101::open> open fd[0x14], flag 7, /private/var/mobile/Containers/Shared/AppGroup/846A9DAC-AB6F-40F2-82D4-A8A3C3A0999E/mmkv/mmkv.default.crc
[I] <MemoryFile.cpp:215::mmap> mmap to address [0x103938000], oldPtr [0x0], [/private/var/mobile/Containers/Shared/AppGroup/846A9DAC-AB6F-40F2-82D4-A8A3C3A0999E/mmkv/mmkv.default.crc]
[I] <MemoryFile_OSX.cpp:35::tryResetFileProtection> protection on [/private/var/mobile/Containers/Shared/AppGroup/846A9DAC-AB6F-40F2-82D4-A8A3C3A0999E/mmkv/mmkv.default.crc] is NSFileProtectionCompleteUntilFirstUserAuthentication
[I] <MMKV_IO.cpp:84::loadFromFile> loading [mmkv.default] with 0 actual size, file size 16384, InterProcess 1, meta info version:0
[I] <MMKV_IO.cpp:133::loadFromFile> loaded [mmkv.default] with 0 key-values
[I] <MMKV_IO.cpp:523::writeActualSize> [mmkv.default] increase sequence to 1, crc 1344674556, actualSize 4

And here are the native logs when react-native-mmkv initializes as version 3.2.0, Expo 52, RN 0.76.7:

[RNMMKV]: Initializing MMKV at /var/mobile/Containers/Data/Application/F6908A6E-1CC3-4BA7-8984-289C6A771650/Documents/mmkv...
[I] <MMKV.cpp:167::initialize> version v2.0.0, page size 16384, arch arm64-v8a
[I] <MMKV.cpp:228::initializeMMKV> root dir: /var/mobile/Containers/Data/Application/F6908A6E-1CC3-4BA7-8984-289C6A771650/Documents/mmkv
[RNMMKV]: Creating MMKV instance "mmkv.default"... (Path: /private/var/mobile/Containers/Shared/AppGroup/846A9DAC-AB6F-40F2-82D4-A8A3C3A0999E, Encrypted: false)
[I] <MMKV.cpp:255::mmkvWithID> prepare to load mmkv.default (id bf6d12c9c9329cf96d113759d2bda5c9) from rootPath /private/var/mobile/Containers/Shared/AppGroup/846A9DAC-AB6F-40F2-82D4-A8A3C3A0999E
[I] <MemoryFile.cpp:101::open> open fd[0x18], flag 7, /private/var/mobile/Containers/Shared/AppGroup/846A9DAC-AB6F-40F2-82D4-A8A3C3A0999E/mmkv.default
[I] <MemoryFile.cpp:215::mmap> mmap to address [0x1056c4000], oldPtr [0x0], [/private/var/mobile/Containers/Shared/AppGroup/846A9DAC-AB6F-40F2-82D4-A8A3C3A0999E/mmkv.default]
[I] <MemoryFile.cpp:101::open> open fd[0x19], flag 7, /private/var/mobile/Containers/Shared/AppGroup/846A9DAC-AB6F-40F2-82D4-A8A3C3A0999E/mmkv.default.crc
[I] <MemoryFile.cpp:215::mmap> mmap to address [0x1056c8000], oldPtr [0x0], [/private/var/mobile/Containers/Shared/AppGroup/846A9DAC-AB6F-40F2-82D4-A8A3C3A0999E/mmkv.default.crc]
[I] <MMKV_IO.cpp:84::loadFromFile> loading [mmkv.default] with 0 actual size, file size 16384, InterProcess 1, meta info version:0
[I] <MMKV_IO.cpp:133::loadFromFile> loaded [mmkv.default] with 0 key-values
[I] <MMKV_IO.cpp:523::writeActualSize> [mmkv.default] increase sequence to 1, crc 394464068, actualSize 4

From the logs, it looks like after upgrading, mmkv is loading from a different path. I've tried adjusting the path manually (only after upgrading), but this doesn't seem to work.

To be sure, I'm not certain this is a bug in the react-native-mmkv package, but it's definitely unintended behavior so I thought I'd raise it here. EDITED: This should be considered a bug as v3 behavior for an iOS app with an app group set which does not set a path in mmkv config differs from the native MMKV implementation.

I'll attempt to reproduce this issue in a sample repo this weekend.

Activity

  1. maintenance-hans commented on Feb 19, 2025

    @maintenance-hans

    Guten Tag, Hans here! 🍻

    Thanks for providing detailed information about your issue with migrating from V2 to V3. It seems like you're facing unintended behavior after upgrading. From ze logs, it's clear that ze MMKV is loading from different paths.

    It's good you plan on reproducing ze issue in a sample repo – that will definitely help narrow things down! Meanwhile, if you could also check if you have set up ze AppGroup correctly and ensure that all paths are consistent, that might also help.

    Feel free to update this issue with your sample repo, and we'll take a look. It's important to resolve these migration problems. Danke for your understanding!

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

  2. JacquesLeupin commented on Feb 20, 2025

    @JacquesLeupin
    Author

    I think this provides a reproducible example: https://github.com/JacquesLeupin/AwesomeProject. Steps to repo:

    1. Run the iOS app, see Store was empty, test value stored log
    2. With app in background, upgrade to react-native to 0.76.7 and react-native-mmkv 3.2.0
    3. Re-install the app on the same device after upgrade
    4. See Store was empty, test value stored log (expected to see Test value already exists in store.. log)
  3. JacquesLeupin commented on Feb 21, 2025

    @JacquesLeupin
    Author

    @mrousavy Any chance you could take a look?

  4. mrousavy commented on Feb 21, 2025

    @mrousavy
    Member

    No sorry. No free time.

  5. JacquesLeupin commented on Feb 22, 2025

    @JacquesLeupin
    Author

    No worries @mrousavy I submitted a PR with a potential fix #813 - would appreciate a review on that if you have a moment!

  6. changed the title [-]Data Loss Migrating from V2 -> V3 with new arch enabled[/-] [+]Data Loss Migrating from V2 -> V3 with app group set on iOS[/+] on Feb 22, 2025
  7. mrousavy commented on Aug 20, 2025

    @mrousavy
    Member

    Closing since it seems like this discussion is resolved in your PR.

    Sorry for the inconvenience on the data loss.

  8. pke commented on Sep 10, 2025

    @pke

    so setting a path while still using mmkv v2 should then make an upgrade to v3 with App groups work flawlessly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions