workmanager listener called immediately

后端 未结 2 1725
春和景丽
春和景丽 2021-02-06 10:39

I need to have a callback upon Work completion from WorkManager (android.arch.work:work-runtime-ktx:1.0.0-alpha11). Yet the listener I\'m adding is called immediately after work

相关标签:
2条回答
  • 2021-02-06 10:59

    Until a proper solution is found for this (w/o GCed errors etc), a rudimentary approach could be to create another WorkRequest and chain it as the last work request to handle the state.

    Ofcourse, you would have to handle the error states separately (And always return Result.success for each WorkRequest) to allow propagation through the chain.

    This is by no means a sustainable approach, but rather a quick fix if necessary

    0 讨论(0)
  • 2021-02-06 11:08

    The problem is not observed if you get the result with liveData like this, instead of using ListenableFuture

    WorkManager.getInstance()
    .getWorkInfoByIdLiveData(cloudSyncOneTimeWork.id)
    .observe(yourLifecycle, yourObserver)
    

    If you don't want to tie it to a lifecycleowner you can call it like this :

    WorkManager.getInstance()
    .getWorkInfoByIdLiveData(cloudSyncOneTimeWork.id)
    .observeForever(yourObserver)
    

    If you do so you are responsible to unregister the listener when you want

    0 讨论(0)
提交回复
热议问题