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

Rob*_*uyl 10 ios react-native asyncstorage

我正在使用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)
Run Code Online (Sandbox Code Playgroud)

Gol*_*Jer 17

当AsyncStorage为空时...

AsyncStorage.clear()
iOS 上有错误,但 Android 上没有。

AsyncStorage.getAllKeys().then(AsyncStorage.multiRemove)
Android 上有错误,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);
  }
}
Run Code Online (Sandbox Code Playgroud)


Rob*_*uyl 15

我认为,当从未使用过存储器时,clear会抛出异常,这很糟糕; 至少那是我的想法是这样的?

所以继续我修改代码,删除.clear()并替换它 AsyncStorage.getAllKeys().then(AsyncStorage.multiRemove)

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