-
-
Notifications
You must be signed in to change notification settings - Fork 267
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Safely migrate from AsyncStorage #52
Comments
Well yeah, you can just use something like this: const hasMigrated = MMKV.getBoolean("hasMigratedFromAsyncStorage")
if (!hasMigrated) {
const keys = await AsyncStorage.getAllKeys();
const values = await AsyncStorage.multiGet(keys);
values.forEach(([key, value]) => {
MMKV.set(key, value)
});
MMKV.set("hasMigratedFromAsyncStorage", true)
await AsyncStorage.clear(); // (optional)
}
|
The migration script is working fine in most cases, but when the apollo-cache-persist item in AsyncStorage is more than 2MB in size, than AsyncStorage.multiGet(keys) will throw an exception. The stacktrace is native C++ pointing to MMKV which is weird and prolonged the investigation of the root cause for us. That's why I wanted to mention here. Below you can find our migration script which wont crash the app if this happens:
|
Might be an out of memory exception for the MMKV instance? Don't forget that MMKV is an in-memory database |
No, it was this exception: https://stackoverflow.com/questions/57014171/react-native-asyncstorage-row-too-big-to-fit-into-cursorwindow |
Here's an up-to-date snippet to migrate from AsyncStorage to MMKV using the latest version of both:
|
The migration script is now added to the Documentation here, so these comments might be outdated. |
May I ask why this migration script is using |
No idea, you can remove it as well |
@mrousavy instead of reading every item you can use multi get: -const value = await AsyncStorage.getItem(key);
+const keys = await AsyncStorage.getAllKeys();
+const entries = await AsyncStorage.multiGet(keys); |
@mrousavy
|
@DePavlenko how can i test if its working or not? i have live app with async storage and new code complety different codebase with mmkv, migrating to mmkv from asyncstorage , I need to find a way to test if your code worked or not can you guide thanks? |
Hello. I really want to replace in my app on all the place AsyncStorage and use MMKV. The problem I have is that I use async storage with redux persist and if I want to change to MMKV I will need to log out users as in redux persist I keep info if the user has a profile, authenticated, etc. Any idea maybe how safely do migration if it is possible?
The text was updated successfully, but these errors were encountered: