Last active
May 14, 2021 19:24
-
-
Save magdamiu/4d1ea5454cf7f57173255b69efcc45d1 to your computer and use it in GitHub Desktop.
WorkManager and LiveData
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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