WorkManager.getInstance(applicationContext).getWorkInfoByIdLiveData(id).observe 是否保证观察 WorkInfo.State.ENQUEUED
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) {
// DO SOMETHING IMPORTANT
}
})
我可以依靠一直观察 WorkInfo.State.ENQUEUED
的状态吗?
或者我的观察者在某些情况下不会呈现这种状态?
是的,尽管与此相关的事情很少需要注意,但可以保证观察到 API。以下是来自 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}.
下面的观察代码应该从主线程调用,否则状态将不是最新的
WorkManager.getInstance(applicationContext).getWorkInfoByIdLiveData(myWorkRequest.id).observe(lifeCycleOwner, Observer {
if (it != null && it.state == WorkInfo.State.ENQUEUED) {
// DO SOMETHING IMPORTANT
}
})
我目前的 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) {
// DO SOMETHING IMPORTANT
}
})
我可以依靠一直观察 WorkInfo.State.ENQUEUED
的状态吗?
或者我的观察者在某些情况下不会呈现这种状态?
是的,尽管与此相关的事情很少需要注意,但可以保证观察到 API。以下是来自 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}.
下面的观察代码应该从主线程调用,否则状态将不是最新的
WorkManager.getInstance(applicationContext).getWorkInfoByIdLiveData(myWorkRequest.id).observe(lifeCycleOwner, Observer {
if (it != null && it.state == WorkInfo.State.ENQUEUED) {
// DO SOMETHING IMPORTANT
}
})