Is WorkManager.getInstance(applicationContext).getWorkInfoByIdLiveData(id).observe guaranteed to observe WorkInfo.State.ENQUEUED

huangapple go评论84阅读模式
英文:

Is WorkManager.getInstance(applicationContext).getWorkInfoByIdLiveData(id).observe guaranteed to observe WorkInfo.State.ENQUEUED

问题

我目前的Android应用程序使用以下代码:

archWorkerRuntimeVersion = '2.3.0-beta02'

api "androidx.work:work-runtime:$archWorkerRuntimeVersion"
api "androidx.work:work-runtime-ktx:$archWorkerRuntimeVersion"

我通过LiveData来观察工作状态,如下所示:

WorkManager.getInstance(applicationContext).getWorkInfoByIdLiveData(myWorkRequest.id).observe(lifeCycleOwner, Observer {
    if (it != null && it.state == WorkInfo.State.ENQUEUED) {
        // 执行重要操作
    }
})

我可以依赖于始终观察到WorkInfo.State.ENQUEUED状态吗?

或者在某些情况下,我的观察者可能不会看到这个状态吗?

英文:

My current Android application employs

archWorkerRuntimeVersion = '2.3.0-beta02'

api "androidx.work:work-runtime:$archWorkerRuntimeVersion"
api "androidx.work:work-runtime-ktx:$archWorkerRuntimeVersion"

I am observing the workers state via LiveData as follows:-

   WorkManager.getInstance(applicationContext).getWorkInfoByIdLiveData(myWorkRequest.id).observe(lifeCycleOwner, Observer {
        if (it != null && it.state == WorkInfo.State.ENQUEUED) {
            // DO SOMETHING IMPORTANT
        }
    })

Can I rely on always observing a state of WorkInfo.State.ENQUEUED?

or could my observer not be presented with this state in some circumstances?

答案1

得分: 1

是的,可以确保观察,但与此API相关的一些注意事项需要牢记。以下是来自Google的描述:

  • 将给定的观察者添加到观察者列表中。此调用类似于具有LifecycleOwner的{@link LiveData#observe(LifecycleOwner, Observer)},LifecycleOwner始终处于活动状态。这意味着给定的观察者将接收所有事件,并且不会自动删除。您应该手动调用{@link #removeObserver(Observer)}来停止观察此LiveData。
  • 只要LiveData具有这样的观察者之一,它将被视为处于活动状态。
  • 如果观察者已经与此LiveData的所有者一起添加,则LiveData会引发{@link IllegalArgumentException}。

而且,下面的观察代码应该从主线程调用,否则状态将不会是最新的:

WorkManager.getInstance(applicationContext).getWorkInfoByIdLiveData(myWorkRequest.id).observe(lifeCycleOwner, Observer {
    if (it != null && it.state == WorkInfo.State.ENQUEUED) {
        // 执行重要操作
    }
})
英文:

Yes, it is guaranteed to observe though there few things that you have to keep on mind related to this API. Below is description is from google:

* Adds the given observer to the observers list. This call is similar to
 * {@link LiveData#observe(LifecycleOwner, Observer)} with a LifecycleOwner, which
 * is always active. This means that the given observer will receive all events and will never
 * be automatically removed. You should manually call {@link #removeObserver(Observer)} to stop
 * observing this LiveData.
 * While LiveData has one of such observers, it will be considered
 * as active.
 * <p>
 * If the observer was already added with an owner to this LiveData, LiveData throws an
 * {@link IllegalArgumentException}.

And below observe code should be called from Main thread otherwise the status will not be latest

WorkManager.getInstance(applicationContext).getWorkInfoByIdLiveData(myWorkRequest.id).observe(lifeCycleOwner, Observer {
    if (it != null && it.state == WorkInfo.State.ENQUEUED) {
        // DO SOMETHING IMPORTANT
    }
})

huangapple
  • 本文由 发表于 2020年1月4日 00:36:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定