Is it possible to remove all items from AsyncStorage

人走茶凉 提交于 2021-02-10 12:38:06

问题


I'm testing my React-Native application and want to remove all items from AsyncStorage in order to test the app from the beginning. And I am a bit confused.

I read the official documentation and found multiRemove and clear functions, but I cannot understand how to clear all items of my application (clear as far as I understood clear the whole storage of all applications and I'm afraid to use it), and multiRemove delete only keys that I give it in parameters, but I want to clear all keys.

I suppose I can do it through getAllKeys keys-values and remove it one-by-one, but maybe there is a more clear way to do it? :)

thanks

P.S: I tried to to like this:

clearAllData() {
    AsyncStorage.multiRemove([]).then(() => alert('success'));
}

but it doesn't work...


回答1:


I suppose I can do it through getAllKeys keys-values and remove it one-by-one, but maybe there is a more clear way to do it? :)

You should do that, that's the only way to remove all keys from your app.

Here is a simple way of doing it:

clearAllData() {
    AsyncStorage.getAllKeys()
        .then(keys => AsyncStorage.multiRemove(keys))
        .then(() => alert('success'));
}



回答2:


removeFew = async () => {
  const keys = ['@MyApp_USER_1', '@MyApp_USER_2']
  try {
    await AsyncStorage.multiRemove(keys)
  } catch(e) {
    // remove error
  }
  console.log('Done')
}



回答3:


Here a way of doing it, using async/await:

const keys = await AsyncStorage.getAllKeys()
await AsyncStorage.multiRemove(keys)


来源:https://stackoverflow.com/questions/56003885/is-it-possible-to-remove-all-items-from-asyncstorage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!