Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WorkManager and LiveData
// by id
WorkManager.getInstance(context).getWorkInfoByIdLiveData(syncOnlyOnce.id)
.observe(this, Observer { workInfo ->
if (workInfo != null &&
workInfo.state == State.SUCCEEDED) {
displayMessage("Sync finished!")
}
})
//by tag
WorkManager.getInstance(context)
.getWorkInfosByTagLiveData(Constants.TAG_SYNC)
.observe(this,
Observer<List<WorkStatus>> { workStatusList ->
val currentWorkStatus = workStatusList ?.getOrNull(0)
if (currentWorkStatus ?.state ?.isFinished == true) {
displayMessage("Sync finished!")
}
})
//by unique name
WorkManager.getInstance(context)
.getWorkInfosForUniqueWorkLiveData(Constants.UNIQUE_NAME)
.observe(this,
Observer<List<WorkStatus>> { workStatusList ->
val currentWorkStatus = workStatusList ?.getOrNull(0)
if (currentWorkStatus ?.state ?.isFinished == true) {
displayMessage("Sync finished!")
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment