Reputation: 631
I'm queuing periodic task in the following way:
PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(UpdateWorker.class, 1, TimeUnit.DAYS)
.addTag(WORK_TAG)
.build();
WorkManager.getInstance().enqueue(request);
How can I check if this periodic task was queued? I've tried:
LiveData<List<WorkStatus>> statusesLiveData = WorkManager.getInstance().getStatusesByTag(WORK_TAG);
but the list seems to be null all the time.
Upvotes: 1
Views: 955
Reputation: 631
Answering my own question: there is actually a way to do this without observing (it should be called from background thread):
List<WorkStatus> statusesLiveData = WorkManager.getInstance().synchronous()
.getStatusesByTagSync(WORK_TAG);
Upvotes: 0
Reputation: 5321
You are not observing the LiveData, since no observer, so no values are being emitted, thus null. Update your code as following:
WorkManager.getInstance().getStatusesByTag("[TAG_STRING]").observe(this, listOfWorkStatuses -> {
WorkStatus workStatus = listOfWorkStatuses.get(0);
boolean finished = workStatus.getState().isFinished();
if (!finished) {
// Work InProgress
} else {
// Work Finished
}
});
Upvotes: 1
Reputation: 1888
You need to observe the LiveData
to get the value. No value will be emitted if there's no observer for it, which is the feature of LiveData
.
LiveData<List<WorkStatus>> statusesLiveData = WorkManager.getInstance().getStatusesByTag(WORK_TAG);
statusesLiveData.observe(...)
Upvotes: 1