React-Native AsyncStorage.clear() is failing on IOS

前端 未结 2 2060
失恋的感觉
失恋的感觉 2021-02-08 01:34

I\'m using AsyncStorage.clear() which is working just fine on Android but when run using iOS platform (real device) I\'m getting this error and can\'t find anything

相关标签:
2条回答
  • 2021-02-08 01:40

    When AsyncStorage is empty...

    AsyncStorage.clear()
    errors on iOS but not Android.

    AsyncStorage.getAllKeys().then(AsyncStorage.multiRemove)
    errors on Android but not iOS.

    Our solution...

    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);
      }
    }
    
    0 讨论(0)
  • 2021-02-08 02:04

    I think it's crappy that clear throws an exception when the storage has never been used; at least that is what I'm thinking is the case?

    So to move on I modified code removing the use of .clear() and replacing it with AsyncStorage.getAllKeys().then(AsyncStorage.multiRemove)

    I'd still like an reason if anyone knows what is going on.

    0 讨论(0)
提交回复
热议问题