将AsyncStorage从react-native迁移到@ react-native-community / async-storage,旧的已保存数据丢失

时间:2019-09-15 11:14:52

标签: javascript react-native expo react-native-ios asyncstorage

我正在将我发布的项目中的AsyncStorage"react-native"迁移到'@react-native-community/async-storage',并且我意识到以前保存的所有数据都丢失了。不幸的是,由于这种迁移,我不能让我的零售用户丢失所有数据。是否可以将用AsyncStorage保存的数据从"react-native"迁移到'@react-native-community/async-storage'?谢谢

2 个答案:

答案 0 :(得分:1)

您可以在第一次检查新存储(来自新库)是否为空,然后尝试使用存在/旧的AsyncStorage库进行导入 到新的:

import { AsyncStorage as OldAsyncStorage}  from 'react-native';

importData = () => {
  OldAsyncStorage.getAllKeys.then((keys) => {
    return OldAsyncStorage.multiGet(keys)
      .then((result) => {
        ...
      });
  });
}

之后,使用multiSet函数将其存储到新存储中。

答案 1 :(得分:0)

我忘了提到我正在使用Expo,现在我只想使用ReactNative。我发现ReactNative核心使用@"RNCAsyncLocalStorage_V1"而不是Expo使用@"RCTAsyncLocalStorage",因此目录路径不匹配。

展览路径 https://github.com/expo/expo/blob/15a7ff76abb149962b1c42f617ca97d3f6c98021/ios/Exponent/Versioned/Core/EXVersionManager.m#L348

反应原生 https://github.com/react-native-community/async-storage/commit/8d275ad706c27962f71849119bc0f1be811a375e#diff-54c57f4041d9a698ab027d21344b6e63