D
D
Denis2019-07-29 14:14:01
Android
Denis, 2019-07-29 14:14:01

How to clear WorkManager activity status after read in android?

Greetings!
When using WorkManager, I encountered the problem that the status of the operation is not removed from the read field.
Here is the initialization of the operation

private fun saveCashCommandSchedule() {
        val constraints = Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build()
        val work = OneTimeWorkRequestBuilder<WorkSynchronizeCashSchedule>()
                .setConstraints(constraints)
                .build()
        WorkManager.getInstance().enqueueUniqueWork("send_cash_schedule", ExistingWorkPolicy.REPLACE, work)
    }

Here I am attaching a listener to keep track of the state of the operation
fun subscribeWorkScheduleCash(lifecycle: Lifecycle) {
        WorkManager.getInstance().getWorkInfosForUniqueWorkLiveData("send_cash_schedule").observe({
            lifecycle
        }, { states ->
            states?.forEach {
                if (it != null && it.state == WorkInfo.State.SUCCEEDED) {
                    //TODO вот этой строки хотелось бы избежать
                   // WorkManager.getInstance().pruneWork()
                    loadData()
                    [email protected]
                }
            }
        })
    }

Every time I connect the listener, the old status pops up. I need to remove it somehow.
Using WorkManager.getInstance().pruneWork() is not an option, as it removes all the statuses of all operations that have completed their work.
Please tell me how to remove the statuses.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Vasilenko, 2019-08-08
@farewell

Try adding a keepResultsForAtLeast call with a duration of 1 millisecond to the OneTimeWorkRequestBuilder.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question