4

I'm trying to get my WorkerInfo with the getWorkInfoByLiveData method that exists in the WorkManager instance.

val workInfo = workManager.getWorkInfoByIdLiveData(uuid).value

The WorkInfo is always null. Also, I'm calling this method from the main thread.

The scenario of how I'm checking this method. I try to enqueue my worker when a user sends a network request and if the internet is not connected I simply register a work with the WorkManager. After some time if I try to get the WorkerInfo with the UUID, it'll always give me null.

Note: When calling getWorkInfoByLiveData the Worker is not executed at that time.

Don't I'm expecting from WorkManager to give me WorkInfo with ENQUEUED State.

Edit 1: So, another scenario would be like this, the app on which I'm working is like a social app. Now after registering a first worker, let's say the user don't want to see the posts from a specific user so this where I need to register my second worker because the user internet is not available at this time. Now what I need to do is to cancel the previously registered worker and then create a Chain of workers with not to show post of a user to beginWith and then the fetch all posts. Now in order to cancel the worker, I check that if the previous worker is still in Enqueued State then cancel it and create a new chain or workers.

Here is the code.

fun Context.isWorkerRegistered(uuid: UUID?): Boolean {
   val id = uuid ?: return false
   val workerInfo = workManager.getWorkInfoByIdLiveData(id).value
   return workerInfo?.state == WorkInfo.State.ENQUEUED
}

The workInfo instance is always null.

Ahsan Saeed
  • 701
  • 10
  • 22

1 Answers1

2

Note: Livedata won't calculate the value until an active observer is added.

getWorkInfoByIdLiveData() returns a LiveData<WorkInfo> that you need to observe to get the workInfo value:

val status = workManager.getWorkInfoByIdLiveData(uuid)
                        .observe(this, Observer{ workInfo ->

    if (workInfo!=null){
        // ...
    }
}

you can take a look at the WorkManager's codelab to see how it can be used.

pfmaggi
  • 6,116
  • 22
  • 43
  • Yes, I know that but the getValue method always returns the current value according to LiveData documentation. So, don't you think when I enqueue the worker on WorManager and after that, it needs to give me WorkInfo with the **UUID**. – Ahsan Saeed Jan 28 '20 at 21:32
  • LiveData is to watch the data and distribute it to the observers. It won't calculate the value until an active observer is added. – pfmaggi Jan 28 '20 at 22:01
  • This article can be useful to understand this topic: https://medium.com/androiddevelopers/unit-testing-livedata-and-other-common-observability-problems-bb477262eb04 – pfmaggi Jan 28 '20 at 22:04
  • Now if you see in the [article](https://medium.com/androiddevelopers/unit-testing-livedata-and-other-common-observability-problems-bb477262eb04) the unit test for this data `assertEquals(viewModel.liveData1.value, "foo") // Passes` is passed even if there are no active observers. Well that's because the getValue always give you the last value. Also, for my case, I have one active observer too but still not getting the value – Ahsan Saeed Jan 28 '20 at 22:10
  • Can you then post some more of the code that you have in your app? when/where are you observing this livedata? – pfmaggi Jan 29 '20 at 07:52
  • Updated the question – Ahsan Saeed Jan 31 '20 at 15:12