0

using "expo" (~43.0.2) and "expo-background-fetch" (~10.0.3), my app need to download update from server then save it into storage (AsyncStorage is simply used), but failed. here is my code snippet

TaskManager.defineTask("test", async () => {
  const token = await AsyncStorage.getItem("token");
  const res = await fetch("...", { headers: { "authorization": token } }) // download data from server
  const data = await res.json();

 
  await AsyncStorage.setItem("unread", `${data?.count ?? 0}`);
  return BackgroundFetch.BackgroundFetchResult[+data?.count ? "NewData" : "NoData"]
})

I find that the AsyncStorage.getItem is working because there is a valid server log. However, i cannot retrieve the unread from AsyncStorage in the app.

can someone help? any suggestion to me?

moreover, i need to call expo.pedometer.getStepCountSync and scheduleNotificationAsync during the background task, is it possible?

Jacky Lau
  • 665
  • 5
  • 21
  • What's with the IIFE for the data and the parse in it? Shouldn't you already have a JSON right then and there in json that you can do things with at `const json = await res.json();` time? – Nelloverflow May 22 '22 at 16:42
  • my fault, I remove the IIFE, but it doesn't help – Jacky Lau May 23 '22 at 02:52

0 Answers0