WorkManager系列(五)Chaining Work

WorkManager allows you to create and enqueue a chain of work that specifies multiple dependent tasks, and defines what order they should run in. This is particularly useful when you need to run several tasks in a particular order.

To create a chain of work, you can use WorkManager.beginWith(OneTimeWorkRequest) or WorkManager.beginWith(List<OneTimeWorkRequest>) , which return an instance of WorkContinuation.

WorkContinuation can then be used to add dependent OneTimeWorkRequests usingWorkContinuation.then(OneTimeWorkRequest) or WorkContinuation.then(List<OneTimeWorkRequest>) .

Every invocation of the WorkContinuation.then(...), returns a new instance of WorkContinuation. If you add a List of OneTimeWorkRequests, these requests can potentially run in parallel.

Finally, you can use the WorkContinuation.enqueue() method to enqueue() your chain of WorkContinuations.

Let's look at an example where an application runs image filters on 3 different images (potentially in parallel), then compresses those images together, and then uploads them.

WorkManager.getInstance()
    // Candidates to run in parallel
    .beginWith(Arrays.asList(filter1, filter2, filter3))
    // Dependent work (only runs after all previous work in chain)
    .then(compress)
    .then(upload)
    // Don't forget to enqueue()
    .enqueue();

Input Mergers

When using chains of OneTimeWorkRequests, the output of parent OneTimeWorkRequests are passed in as inputs to the children. So in the above example, the outputs of filter1filter2 and filter3 would be passed in as inputs to the compress request.

In order to manage inputs from multiple parent OneTimeWorkRequests, WorkManager uses InputMergers.

There are two different types of InputMergers provided by WorkManager:

OverwritingInputMerger attempts to add all keys from all inputs to the output. In case of conflicts, it overwrites the previously-set keys.

ArrayCreatingInputMerger attempts to merge the inputs, creating arrays when necessary.

For the above example, given we want to preserve the outputs from all image filters, we should use an ArrayCreatingInputMerger.

OneTimeWorkRequest compress =
    new OneTimeWorkRequest.Builder(CompressWorker.class)
        .setInputMerger(ArrayCreatingInputMerger.class)
        .setConstraints(constraints)
        .build();

Chaining and Work Statuses

There are a couple of things to keep in mind when creating chains of OneTimeWorkRequests.

Dependent OneTimeWorkRequests are only unblocked (transition to ENQUEUED), when all its parent OneTimeWorkRequests are successful (that is, they return a Result.success()).

When any parent OneTimeWorkRequest fails (returns a Result.failure(), then all dependent OneTimeWorkRequests are also marked as FAILED.

When any parent OneTimeWorkRequest is cancelled, all dependent OneTimeWorkRequests are also marked as CANCELLED.

For more information, see Cancelling and Stopping Work.

------------------------------------------------------------------

这一片简单总结一下,四个字:“链式调用”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值