# Mini Game Update

Mini Game can start in two situations, one is "cold start" and the other is "hot start." If the user has already opened a Mini Game and then opens the Mini Game again within a certain period of time, there is no need to restart it, just switch the Mini Game from background to the foreground, and this process is hot start; cold start refers to the user opening the Mini Game for the first time or the Mini Game is destroyed by Weixin and then opened again, at this time the game needs to be reloaded to start.

# Update Mechanism

If a new version is found during the cold start of the Mini Game, the new version of the code package will be downloaded asynchronously and will be started with the client's existing local package. That is, the new version of the Mini Game needs to wait until the next cold start to apply. If you need to apply the latest version immediately, you can use the wx.getUpdateManager() API for processing.

# Example of Using getUpdateManager

For the base library v1.9.90 and above, you can use wx.getUpdateManager() to get the globally unique version update manager for managing Mini Game updates; also please download the latest version of the Developer Tools (only versions above 1.02.1803130 support debugging on Developer Tools).

Since it is the API supported by the new version, please confirm whether it is supported before use, for example:

if (wx.getUpdateManager) {
  console.log('support wx.getUpdateManager')
}

After getting the updateManager instance, updateManager contains the following methods:

Method Parameter Description
onCheckForUpdate callback Callback when requesting new version information from the Weixin background is completed
onUpdateReady callback Callback when the new version is downloaded
onUpdateFailed callback Callback when a new version downloading fails
applyUpdate When the new version is downloaded, calling this method will force the current Mini Game to apply the new version and restart

onCheckForUpdate(callback) callback result description:

Note: There are BUGs that no callback for onCheckForUpdate in some cases for Android 6.6.6 and below, it is recommended to use this callback only with version 6.6.7 and above for Android

Properties Type Description
hasUpdate Boolean Is there a new version

Note: The Check for Update operation is automatically triggered by Weixin at the cold start of the Mini Game. It does not need to be triggered by the developer and the developer only needs to subscribe the check result.

onUpdateReady(callback) callback result description:

When Weixin checks that there is a new version of the Mini Game, it will trigger the download operation (no need triggering from developer). When the download is completed, the developer will be notified via onUpdateReady.

onUpdateFailed(callback) callback result description:

When Weixin checks that there is a new version of the Mini Game, it will trigger the download operation (no need triggering from developer). If the downloading fails (possibly due to network reasons, etc.), the developer will be notified via onUpdateFailed.

applyUpdate() description:

When the new version of the Mini Game has been downloaded (that is, the onUpdateReady` callback is received), this method can be used to force the restart of the Mini Game and apply the latest version.

# Complete Example of Use

if (typeof wx.getUpdateManager === 'function') {
  const updateManager = wx.getUpdateManager()

  updateManager.onCheckForUpdate(function (res) {
    // The request for callback of new version information is completed
    console.log(res.hasUpdate)
  })

  updateManager.onUpdateReady(function () {
    // The new version has been downloaded and available, call applyUpdate to apply the new version and restart
    updateManager.applyUpdate()
  })

  updateManager.onUpdateFailed(function () {
    // New version failed to be downloaded
  })
}
点击咨询小助手