AsyncStorage 没有返回回调

AsyncStorage is not returning the callback

我在 React Native 项目中使用 redux-persist,运行 在很多设备上都很好 除了 Android 7.我正在尝试调试关于为什么我的本地存储不持久的问题,并发现了这个:

以下代码在 React Native 组件生命周期内执行

  componentDidMount() {
    attachObservables(store)
    setInterval(async () => {
      console.log('Inside setInterval')
      const data = await AsyncStorage.getAllKeys()
      console.log('inside the getAllKeys')
      data.forEach(async k => {
        const value = await AsyncStorage.getItem(k)
        console.group(k)
        console.log(value)
        console.groupEnd()
      })
    }, 3000)
  }

'Inside setInterval' 之后的代码永远不会被调用。如果在 setInterval 之外,它只会 运行s 一次。如果我调用一次代码 outside setInterval 它似乎 运行 就好了。我还尝试了回调格式与异步/等待版本,但这似乎并不重要。

我在使用 firebase js 库时遇到了同样的问题(回调从不在第一个之后 return)。我现在正在寻找解决该问题的替代方案。

有什么想法吗?

从某些 Android 版本的 React Native 0.51 开始,运行时可能会被其他本机模块阻塞,从而阻碍上述方法的解析。

它可以通过 https://github.com/facebook/react-native/issues/14101#issuecomment-345563563 修复,确保此方法使用线程池中的空闲线程。

已提交PR,希望在以后的版本中发布。如果您希望它立即使用此修复程序,可在此处获得 https://github.com/netbeast/react-native

编辑: 我在 react-native@0.53 上的真实设备上不再遇到这种情况,无论如何其他人也报告过,所以问题仍然悬而未决。