Syncing a Portal with Live Updates
The sync operation checks Appflow for a new version of a web app used in a Portal. If an update is available, the files are downloaded and the Portal is updated to use those new files the next time it loads. The Live Updates SDK will perform a sync when the Live Update Config is added to a Portal by default. This is typically done when an app is initially launched, and requires a full restart of an app to trigger subsequent syncs. We recommend performing a sync in other situations to provide more chances for Portals to update.
Triggering a Sync
A sync can be triggered by calling the sync function in the Live Update Manager.
- Kotlin
- Java
// Sync all configured apps
LiveUpdateManager.sync(context)
// Sync a specific app
PortalManager.sync(context, "appId")
// Sync specific apps
PortalManager.sync(context, arrayOf("appId1", "appId2"))
// Sync all configured apps and callback
LiveUpdateManager.sync(context, callback = object : SyncCallback {
override fun onAppComplete(syncResult: SyncResult) {
Log.d("LiveUpdate","CALLBACK: Sync success for app ${syncResult.liveUpdate.appId}!")
}
override fun onAppComplete(failResult: FailResult) {
Log.e("LiveUpdate","CALLBACK: Sync failed at step ${failResult.failStep.name} for app ${failResult.liveUpdate.appId}!")
}
override fun onSyncComplete() {
Log.d("LiveUpdate","CALLBACK: Sync finished!")
}
})
// Sync all configured apps
LiveUpdateManager.sync(context);
// Sync a specific app
PortalManager.sync(context, "appId");
// Sync specific apps
LiveUpdateManager.sync(this, new String[] {"appId1", "appId2"});
// Sync a specific app and callback
LiveUpdateManager.sync(this, "appId", new SyncCallback() {
@Override
public void onAppComplete(@NonNull SyncResult syncResult) {
Log.d("LiveUpdate","CALLBACK: Sync success for app " + syncResult.getLiveUpdate().getAppId());
}
@Override
public void onAppComplete(@NonNull FailResult failResult) {
Log.d("LiveUpdate","CALLBACK: Sync failed at step " + failResult.getFailStep().name() + " for app " + failResult.getLiveUpdate().getAppId());
}
@Override
public void onSyncComplete() {
Log.d("LiveUpdate","CALLBACK: Sync finished!");
}
});
When to Sync
Deciding when to sync is at your discretion.
Depending on the size of your web app assets, a sync operation could be expensive. Keep in mind that mobile users may be on a cellular network data connection or may be opening the app from a mininized or background state to use it.
The following example performs a sync when an app resumes as long as six hours has elapsed since the previous sync. This ensures a check is performed every time a user opens the app whether it is opened for the first time or opened from a minimized state.
- Kotlin
- Java
override func viewDidLoad() {
// If it has been more than 6 hours since last update check, sync now.
if let lastUpdate = LiveUpdateManager.shared.lastSync(for: "appId"),
let hoursSinceLastUpdate = Calendar.current
.dateComponents([.hour], from: lastUpdate, to: Date()).hour,
hours > 6 {
LiveUpdateManager.shared.sync(appId: "appId")
}
}
// Placed in an Android Activity
override fun onResume() {
super.onResume()
// If it has been more than 6 hours since last update check, sync now.
val lastUpdateTime = LiveUpdateManager.getLastSync(this)
val now = System.currentTimeMillis()
val sixHours = 6 * 60 * 60 * 1000
if(lastUpdateTime < (now - sixHours)) {
LiveUpdateManager.sync(this)
}
}
// Placed in an Android Activity
@Override
protected void onResume() {
super.onResume();
// If it has been more than 6 hours since last update check, sync now.
long lastUpdateTime = LiveUpdateManager.getLastSync(this);
long now = System.currentTimeMillis();
long sixHours = 6 * 60 * 60 * 1000;
if (lastUpdateTime < (now- sixHours)) {
LiveUpdateManager.sync(this);
}
}