You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here's a migration script to copy all values from AsyncStorage to MMKV, and delete them from AsyncStorage afterwards.
storage.ts
importAsyncStoragefrom'@react-native-async-storage/async-storage';import{MMKV}from'react-native-mmkv';exportconststorage=newMMKV();// TODO: Remove `hasMigratedFromAsyncStorage` after a while (when everyone has migrated)exportconsthasMigratedFromAsyncStorage=storage.getBoolean('hasMigratedFromAsyncStorage',);// TODO: Remove `hasMigratedFromAsyncStorage` after a while (when everyone has migrated)exportasyncfunctionmigrateFromAsyncStorage(): Promise<void>{console.log('Migrating from AsyncStorage -> MMKV...');conststart=global.performance.now();constkeys=awaitAsyncStorage.getAllKeys();for(constkeyofkeys){try{constvalue=awaitAsyncStorage.getItem(key);if(value!=null){if(['true','false'].includes(value)){storage.set(key,value==='true');}else{storage.set(key,value);}AsyncStorage.removeItem(key);}}catch(error){console.error(`Failed to migrate key "${key}" from AsyncStorage to MMKV!`,error,);throwerror;}}storage.set('hasMigratedFromAsyncStorage',true);constend=global.performance.now();console.log(`Migrated from AsyncStorage -> MMKV in ${end-start}ms!`);}
App.tsx
...
import{hasMigratedFromAsyncStorage,migrateFromAsyncStorage}from'./storage';
...
exportdefaultfunctionApp(){// TODO: Remove `hasMigratedFromAsyncStorage` after a while (when everyone has migrated)const[hasMigrated,setHasMigrated]=useState(hasMigratedFromAsyncStorage);
...
useEffect(()=>{if(!hasMigratedFromAsyncStorage){InteractionManager.runAfterInteractions(async()=>{try{awaitmigrateFromAsyncStorage()setHasMigrated(true)}catch(e){// TODO: fall back to AsyncStorage? Wipe storage clean and use MMKV? Crash app?}});}},[]);if(!hasMigrated){// show loading indicator while app is migrating storage...return(<Viewstyle={{justifyContent: 'center',alignItems: 'center'}}><ActivityIndicatorcolor="black"/></View>);}return(<YourAppsCode/>);}