React-Native AsyncStorage.clear() 在 IOS 上失败



我正在使用AsyncStorage.clear()它在Android上运行良好,但是当使用iOS平台(真实设备)运行时,我收到此错误并且无法在线找到有关它的任何内容。

Error: Failed to delete storage directory.Error Domain=NSCocoaErrorDomain Code=4 "“RCTAsyncLocalStorage_V1” couldn’t be removed." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/281A6456-6CB2-47D4-AD04-3EB26A1B9506/Documents/RCTAsyncLocalStorage_V1, NSUserStringVariant=(
    Remove
), NSUnderlyingError=0x174049480 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
Error: Failed to delete storage directory.Error Domain=NSCocoaErrorDomain Code=4 "“RCTAsyncLocalStorage_V1” couldn’t be removed." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/281A6456-6CB2-47D4-AD04-3EB26A1B9506/Documents/RCTAsyncLocalStorage_V1, NSUserStringVariant=(
    Remove
), NSUnderlyingError=0x174049480 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
    at convertError (http://192.168.1.33.xip.io:8081/index.ios.bundle?platform=ios&dev=true&minify=false:52255:13)
    at http://192.168.1.33.xip.io:8081/index.ios.bundle?platform=ios&dev=true&minify=false:52109:18
    at MessageQueue.__invokeCallback (http://192.168.1.33.xip.io:8081/index.ios.bundle?platform=ios&dev=true&minify=false:2060:16)
    at http://192.168.1.33.xip.io:8081/index.ios.bundle?platform=ios&dev=true&minify=false:1853:15
    at MessageQueue.__guard (http://192.168.1.33.xip.io:8081/index.ios.bundle?platform=ios&dev=true&minify=false:1991:9)
    at MessageQueue.invokeCallbackAndReturnFlushedQueue (http://192.168.1.33.xip.io:8081/index.ios.bundle?platform=ios&dev=true&minify=false:1852:13)
    at t (file:///Applications/React%20Native%20Debugger.app/Contents/Resources/app.asar/js/RNDebuggerWorker.js:1:14632)

我认为当存储从未使用过时,清除会引发异常是很糟糕的; 至少我是这么想的?

因此,为了继续前进,我修改了代码,删除了 .clear() 的使用并将其替换为 AsyncStorage.getAllKeys().then(AsyncStorage.multiRemove)

如果有人知道发生了什么,我仍然想要一个理由。

当 AsyncStorage 为空时...

AsyncStorage.clear() iOS上的错误,但不是Android上的错误。

AsyncStorage.getAllKeys().then(AsyncStorage.multiRemove) 安卓上的错误,但不是iOS上的错误。

我们的解决方案...

import {  Platform } from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
const asyncStorageKeys = await AsyncStorage.getAllKeys();
if (asyncStorageKeys.length > 0) {
  if (Platform.OS === 'android') {
    await AsyncStorage.clear();
  }
  if (Platform.OS === 'ios') {
    await AsyncStorage.multiRemove(asyncStorageKeys);
  }
}

我正在使用身份验证循环来登录用户,因此将其放在包装整个应用程序的身份验证上下文中

这是我的进口

import AsyncStorage from '@react-native-async-storage/async-storage';
import { Platform } from 'react-native';

并且在AuthContext创建调用之后稍微低一点

const AuthContext = createContext<IAuthProps>({});
const clearStorage = async () => {
  const asyncStorageKeys = await AsyncStorage.getAllKeys();
  if (asyncStorageKeys.length > 0) {
    if (Platform.OS === 'android') {
      await AsyncStorage.clear();
    }
    if (Platform.OS === 'ios') {
      await AsyncStorage.multiRemove(asyncStorageKeys);
    }
  }
};
clearStorage();

我没有使用社区异步存储包,但它仍然可以工作

  • 我知道该函数被标记为异步,但我的项目抛出了一个错误,即不允许在不更改我的 tsconfig 的情况下在顶层await,由于这样做的未知影响,我不想这样做!

相关内容

  • 没有找到相关文章

最新更新