Updating with Updates API


#1

Moved from: https://github.com/expo/expo/issues/1652

James asks:

I want my app to

Don’t check updates before app is loaded (so app is loaded asap)
Check for updates whenever app is opened, and auto refresh the app immediately if an update is downloaded
Would the following code achieve what I want?

fallbackToCacheTimeout: 0

AppState.addEventListener('change', async () => {
  const { isAvailable } = await Updates.checkForUpdateAsync();
  if (isAvailable) {
    await Updates.fetchUpdateAsync();
    Updates.reload(); // i assume this will use the above fetched content instead of re-fetching
  }
});

Should I set checkAutomatically to ON_ERROR_RECOVERY too to prevent double checking since I’m already checking it in the above code?


#2

Hi - yes, the code above should do what you want, and you’re right about setting checkAutomatically: ON_ERROR_RECOVERY to prevent from downloading twice.

However, another (perhaps easier) way to achieve the same thing is to set

fallbackToCacheTimeout: 0,
checkAutomatically: ON_LOAD

and add an event listener to your app as soon as it mounts, since when an update is downloaded it will emit an event. Then you can reload without having to handle downloading the code - e.g.

componentDidMount () {
  Updates.addEventListener(event => {
    if (event.type === Updates.EventType.DOWNLOAD_FINISHED) {
      // ... maybe prompt user
      Updates.reload()
    }
  })
}

Hope that helps – let me know if you have any other questions!


#3

checkAutomatically: ON_LOAD will only check when app is started, not when app is resumed from background. So if the user uses ur app regularly and never close it from background, it will never be updated.

My code does the check whenever it’s resumed from the background.

Also, why do you put the code in componentDidMount? Since it’s async, putting it outside of the component class (like where you declare const styles = StyleSheet.create(...)) would have negligible effect on performance?


#4

Does Updates.reload() really use the already downloaded version?

If so why is Updates.reloadFromCache() added in sdk 27?


closed #5

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


#6

Hi @james2412 - sorry for the delayed response. Updates.reload will make the app act as if it were starting up for the first time again, so it will use whatever updates configuration is in app.json to load the app. So if you have a non-zero fallbackToCacheTimeout, Updates.reload will try and fetch the newest version from the server each time you call it.

On the other hand, Updates.reloadFromCache will use the already downloaded version regardless of your updates settings in app.json.


Expo.Updates.reload() behavior