Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsyncStorage is not returning the callback

Tags:

I am using redux-persist in a react native project, that runs just fine in a broad number of devices except Android 7. I am trying to debug the problem on why my local storage is nor persisting and found this:

The following code executes inside React Native component lifecycle's

  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)
  }

Code after 'Inside setInterval' is never called. It only runs once if outside the setInterval. If I call once the code outside the setInterval it appears to run just fine. I also tried callback format vs async / await version but it does not seem to matter.

Same problem I had using firebase js library (callbacks never return after the first one). I am now looking for alternatives to workaround the problem.

Any ideas?

like image 559
jsdario Avatar asked May 22 '17 13:05

jsdario


People also ask

What does AsyncStorage getItem return?

getItem ​ Gets a string value for given key . This function can either return a string value for existing key or return null otherwise. In order to store object value, you need to deserialize it, e.g. using JSON. parse() .

Is AsyncStorage deprecated?

Deprecated. Use one of the community packages instead. AsyncStorage is an unencrypted, asynchronous, persistent, key-value storage system that is global to the app. It should be used instead of LocalStorage.

Does AsyncStorage persist?

AsyncStorage is also asynchronous, meaning that its methods run concurrently with the rest of your code, and it's persistent, meaning that the stored data will always be available globally even if you log out or restart the application.


1 Answers

As of React Native 0.51 in some Android versions, the runtime can get blocked by other native modules, impeding the resolution of the mentioned methods.

It can be fixed via https://github.com/facebook/react-native/issues/14101#issuecomment-345563563, ensuring this methods use a free thread from the thread pool.

A PR has been submitted and I hope that will be released in future versions. If you want it to use this fix right now, it is available here https://github.com/netbeast/react-native

EDIT: I am not experiencing this anymore on real devices over [email protected], anyhow others have also reported, so the issue is still open.

like image 156
jsdario Avatar answered Sep 23 '22 10:09

jsdario