-
Notifications
You must be signed in to change notification settings - Fork 482
Description
What happened?
Hi
I'm currently planning a migration to AsyncStorage v3 and had a question about how existing data should be handled.
I read the migration guide here:
https://react-native-async-storage.github.io/3.0/migration-to-3/
From my understanding, the new v3 API uses createAsyncStorage() and introduces namespaced storage instances. However, it's not completely clear to me how existing data stored using the old API should be accessed or migrated.
My concern
If a user already has data stored with the previous API (@react-native-async-storage/async-storage v2), is there a way to access that data using the new v3 storage instance, or is a manual migration step required?
For example, I implemented the following migration logic:
const storage = createAsyncStorage("database");
const hasMigrated = await storage.getItem("__migrated__");
if (!hasMigrated) {
const keys = await RNAsyncStorage.getAllKeys();
if (keys.length > 0) {
const entries = await RNAsyncStorage.getMany(keys);
const nonNullEntries: Record<string, string> = {};
for (const key in entries) {
const value = entries[key];
if (value != null) {
nonNullEntries[key] = value;
}
}
await storage.setMany(nonNullEntries);
await RNAsyncStorage.removeMany(keys);
}
await storage.setItem("__migrated__", "true");
}Questions
- Is this kind of manual migration approach the recommended way to move data from the old API to v3?
- Is there any way for the new v3 storage instance to read existing keys directly, or are they completely separate storage namespaces?
- If manual migration is required, does that mean this migration code needs to remain in the codebase indefinitely for users who might update from very old app versions?
- Are there any best practices recommended by the maintainers for handling this migration safely in production apps?
My main goal is to ensure users don't lose previously stored data when upgrading to v3.
Thanks in advance for any clarification!
Version
3
What platforms are you seeing this issue on?
- Android
- iOS
- macOS
- Windows
- web
System Information
React Native 0.84.1