AsyncStorage doesn't work on background task

Hi.
I created a background task using expo for the location updates like expo described here:
https://docs.expo.io/versions/latest/sdk/location/
I want to send the server the user location so I need to read the access token from the device storage.

Something like this:

defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
  const accessToken = await AsyncStorage.getItem('accessToken')
  fetch(SERVER_URL + '/' + 'update_user_location', {
        method: 'POST',
        headers: {
            Authorization: 'Bearer ' + accessToken
        },
        body: data
   }
})

The problem is that await AsyncStorage.getItem('accessToken') returns null if the task runs in killed mode, when the app is open/background it works fine!

I also tried to replace the AsyncStorage with using the FileSystem (write and read the accessToken to a file) and it also didn’t worked it wrote for me something like “File … could not be found”, and also try to use SQLite of expo and also didn’t help, it wrote for me “table items didn’t found” while when the app is running it works fine.

I start thinking that when the task runs in killed mode it doesn’t look for the right path or it doesn’t have permissions to read from the device storage.

What am I missing ?

thanks.

2 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.