UpdateManager
Introduction
The UpdateManager object is used for managing updates and can be obtained through the dlt.getUpdateManager
interface.
Methods
UpdateManager.applyUpdate()
Forcefully restarts the mini-program and uses the new version. Call this method when the new version of the mini-program has been downloaded (i.e., when theonUpdateReady
callback is received).UpdateManager.onCheckForUpdate(function listener)
Listens for events related to checking for update results from the backend. The mini-program automatically checks for updates on every launch, including hot restarts, without the need for developer-triggered actions.UpdateManager.onUpdateReady(function listener)
Listens for events when the mini-program has an update available. The client initiates the download automatically (without developer intervention), and the callback is triggered upon a successful download.UpdateManager.onUpdateFailed(function listener)
Listens for events when the mini-program update process fails. When a new version is available for the mini-program, the client initiates the download automatically (without developer intervention). If the download fails (possibly due to network issues), this callback is triggered.UpdateManager.forceUpdateAppInfo()
Actively initiates an AppInfo request to obtain update result information.UpdateManager.downloadNew()
Actively initiates the download of the latest mini-program package. This is effective after actively initiating an AppInfo request usingUpdateManager.forceUpdateAppInfo()
.
Sample Code
const updateManager = dlt.getUpdateManager();
// 1. Automated update process
updateManager.onCheckForUpdate(function (res) {
// Callback after requesting new version information
console.log(res.hasUpdate);
});
updateManager.onUpdateReady(function () {
dlt.showModal({
title: 'Update Prompt',
content:
'A new version is ready. Would you like to restart the application?',
success: function (res) {
if (res.confirm) {
// The new version has been downloaded; call applyUpdate to apply the new version and restart.
updateManager.applyUpdate();
}
},
});
});
updateManager.onUpdateFailed(function () {
// New version download failed
});
// 2. Actively initiate the update process
updateManager.forceUpdateAppInfo({
success: (res) => {
// Check for a new version update
if (res.hasUpdate) {
// Actively initiate the download of the latest mini-program package
updateManager.downloadNew({
success: (res) => {
if (res.hasReady) {
dlt.showModal({
title: 'Update Prompt',
content:
'A new version is ready. Would you like to restart the application?',
success: function (res) {
if (res.confirm) {
// The new version has been downloaded; call applyUpdate to apply the new version and restart.
updateManager.applyUpdate();
}
},
});
}
},
fail: (res) => {},
complete: (res) => {},
});
}
},
fail: (res) => {},
complete: (res) => {},
});