当前位置: 首页 > 知识库问答 >
问题:

上一版本react-native和AsyncStorage丢失的数据

云隐水
2023-03-14

当我试图用react-native和expo更新我的应用程序(Android/iOS)时,我的数据丢失了,我不知道为什么:

旧版本:

与expo一起构建更新版本

我如何保存数据。代码在不同版本之间没有变化。

  useEffect(() => {
    persistStore(
      store,
      {
        storage: AsyncStorage,
        whitelist: ['data', 'data2' ],
        transforms: [
          createTransform(
            state => state,
            state => ({
              ...state,
              infosOpened: false,
            }),
            {
              whitelist: 'app',
            }
          ),
        ],
      },
      () => {
        setRehydrated(true)
      }
    )
  }, [])

AsyncStorage是从react-native导入的,我用Expo保留react-native。@react-native-community/async-storage不兼容。

正如我在旧版本中看到的那样,数据在“数据用户”中使用SQLlite format 3持久化。数据似乎总是与较新的版本在一起,但不再恢复。

我不知道是否有一些制动变化反应-从版本51到61原生。我在github发布标签中什么也没看到。

https://github.com/expo/expo/issues/8220#issuecomment-656300244

共有1个答案

池照
2023-03-14

您可以在代码段中找到用于迁移的示例代码。这个片段是从Expo到纯RN的迁移。您只需反向迁移即可。

import React, { Component } from 'react';
import { View, ActivityIndicator, AsyncStorage } from 'react-native';
import PropTypes from 'prop-types';
import RNFS from 'react-native-fs';

export default class WalletMigrate extends Component {
  componentDidMount() {
    this.migrateDataFromExpo();
  }

  migrationComplete() {
    console.log('Migration was successful. Exiting migration...')
    this.props.onComplete();
  }

  // Migrate Document directory from Expo
  async migrateDataFromExpo() {
    const expoDirectoryExists = await RNFS.exists(RNFS.DocumentDirectoryPath + '/ExponentExperienceData');

    if (!expoDirectoryExists) {
        console.log('Expo data was previously migrated. Exiting migration...');
      this.props.onComplete();
      return;
    }
    try {
        await RNFS.unlink(RNFS.DocumentDirectoryPath + '/RCTAsyncLocalStorage_V1')
        console.log('/RCTAsyncLocalStorage_V1 has been deleted. Continuing...')
    } catch {
        console.log('/RCTAsyncLocalStorage_V1 does not exist. Continuing...')
    }
    RNFS.copyFile(
      RNFS.DocumentDirectoryPath + '/ExponentExperienceData/%40USERNAME%2FAPPNAME/RCTAsyncLocalStorage',
      RNFS.DocumentDirectoryPath + '/RCTAsyncLocalStorage_V1',
    )
      .then(() => {
        RNFS.readDir(RNFS.DocumentDirectoryPath + '/RCTAsyncLocalStorage_V1').then(files => {
          files.forEach(file => {
            if (file.name !== 'manifest.json') {
              RNFS.readFile(file.path).then(fileContents => {
                AsyncStorage.setItem('data', fileContents)
                .then(() => {
                  RNFS.unlink(RNFS.DocumentDirectoryPath + '/ExponentExperienceData').then(() => this.migrationComplete());
                })
                .catch(() => {
                    console.log('An error was encountered when trying to delete /ExponentExperienceData. Exiting migration...');
                    this.props.onComplete();
                })
                .then(() => this.migrationComplete())
              });
            }
          });
        })
        .catch(error => {
            console.log('An error was encountered when trying to read the /RTCAsyncLocalStorage_V1 directory. Exiting migration...');
            console.log(error);
            this.props.onComplete();
        });
      })
      .catch(_error => this.props.onComplete());
  }

  render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignContent: 'center' }}>
        <ActivityIndicator />
      </View>
    );
  }
}

WalletMigrate.propTypes = {
  onComplete: PropTypes.func,
};
 类似资料:
  • 本文向大家介绍react-native中AsyncStorage实例详解,包括了react-native中AsyncStorage实例详解的使用技巧和注意事项,需要的朋友参考一下 react-native中AsyncStorage实例详解 AsyncStorage是一个简单的,具有异步特性的储存API,它的储存方式为键值对的方式,且对整个App而言,是全局的。 AsyncStorage提供了较全的

  • 我在一个反应原生的应用程序,当我试图建立一个签署的版本,我得到这个错误: ps:buildToolsVersion=“28.0.3”minSdkVersion=21 compileSdkVersion=28 targetSdkVersion=28 supportLibVersion=“28.0.0”

  • 问题内容: 我正在尝试创建一个新的react native项目,该项目应该使用旧版本的react-native。 我想要的结果将是做类似的事情:但有使用它的react-native版本。 但是,似乎没有任何选项可用于使用旧版本的react-native进行初始化。 执行然后降级react-native 也不起作用,因为该命令会安装一堆用于构建应用程序的xcode模板,并且没有命令可以降级这些模板。

  • 当我试图在ios模拟器上运行应用程序时,我得到了这个。有谁能告诉我这个问题的原因/解决办法是什么。 以下构建命令失败:CompileC/users/xxx/library/developer/xcode/deriveddata/eattog-dfbqbeyhmbgzhfcodvhcaqjxepf/build/intermediates.noindex/p s.llvm.clang.1_0.comp

  • 在此版本中使用了不推荐使用的Gradle功能,使其与Gradle 6.0不兼容。使用'--warning-mode all‘显示单个弃用警告。参见https://docs.gradle.org/5.5/userguide/command_line_interface.html#sec:command_line_warnings 26个可操作任务:2个已执行,24个最新 失败:生成失败,出现异常。

  • 问题内容: 我通过世博会创建了该应用程序。我通过应用程序加载程序上传了ipa,但下面的消息被拒绝了。问题是什么? 该捆绑软件无效-/Payload/ExpoKitApp.app/FacebookSDKStrings.bundle的Info.plist文件丢失或无法读取。 无效的捆绑软件-“ /Payload/ExpoKitApp.app/FacebookSDKStrings.bundle”中的捆绑