Jetpack之WorkManager 的链式调度

2021-05-27  本文已影响0人  QiShare

简介

image

使用入门

dependencies {
  def work_version = "2.5.0"
    // (Java only)
    implementation "androidx.work:work-runtime:$work_version"
    // Kotlin + coroutines
    implementation "androidx.work:work-runtime-ktx:$work_version"
    // optional - RxJava2 support
    implementation "androidx.work:work-rxjava2:$work_version"
    // optional - GCMNetworkManager support
    implementation "androidx.work:work-gcm:$work_version"
    // optional - Test helpers
    androidTestImplementation "androidx.work:work-testing:$work_version"
    // optional - Multiprocess support
    implementation "androidx.work:work-multiprocess:$work_version"
  }
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

/**
        * Created by caining on 2021/5/24 14:28
   */
   public class WorkA extends Worker {
   public WorkA(@NonNull Context context, @NonNull WorkerParameters workerParams) {
       super(context, workerParams);
   }

   @NonNull
   @Override
   public Result doWork() {
       long millis = System.currentTimeMillis();
       try {
           Thread.sleep(1000);
       } catch (InterruptedException ignore) {
           return Result.failure();
       }
       long end = System.currentTimeMillis();
       Log.i("Work--------> ", "\nA开始时间:" + millis + "----执行时间:" + String.valueOf(end - millis)+ "----结束时间:" +end);
       return Result.success();
   }
   }
image

链式调度示例

image
OneTimeWorkRequest requestWorkA = new OneTimeWorkRequest.Builder(WorkA.class).build();
OneTimeWorkRequest requestWorkB = new OneTimeWorkRequest.Builder(WorkB.class).build();
OneTimeWorkRequest requestWorkC = new OneTimeWorkRequest.Builder(WorkC.class).build();
OneTimeWorkRequest requestWorkD = new OneTimeWorkRequest.Builder(WorkD.class).build();
OneTimeWorkRequest requestWorkE = new OneTimeWorkRequest.Builder(WorkE.class).build();
  /**
         A       B       C
         |       |       | 
         +-------+-------+
                 |
                 D
     */
WorkManager.getInstance(MainActivity.this)
        .beginWith(Arrays.asList(requestWorkA, requestWorkB, requestWorkC))
        .then(requestWorkD)
        .enqueue();

/**
    example1:执行结果如下
  I/Work-------->: A开始时间:1621843840447----执行时间:1000----结束时间:1621843841447
  I/Work-------->: B开始时间:1621843840448----执行时间:2001----结束时间:1621843842449
  I/Work-------->: C开始时间:1621843840456----执行时间:3000----结束时间:1621843843456
  I/Work-------->: D开始时间:1621843843539----执行时间:1000----结束时间:1621843844539
*/

从时间结果可以看出 执行顺序是 abc 同时开始,执行结束后执行D 任务,符合流程图所执行的链式任务

image
    /**
         A       B   D
         |       |   |
         +-------+   |
             |       |
             C       |
             |       |
             +-------+
                 |
                 E
      `WorkContinuation.combine` 合并混合两个任务
     */

WorkManager workManager = WorkManager.getInstance(MainActivity.this);
WorkContinuation firstABC = workManager.beginWith(Arrays.asList(requestWorkA,requestWorkB)).then(requestWorkC);
WorkContinuation dWork = workManager.beginWith(requestWorkD);
WorkContinuation.combine(Arrays.asList(firstABC, dWork)).then(requestWorkE).enqueue();
I/Work-------->: A开始时间:1621848348002----执行时间:1000----结束时间:1621848349002
I/Work-------->: D开始时间:1621848348003----执行时间:1000----结束时间:1621848349003
I/Work-------->: B开始时间:1621848348003----执行时间:2000----结束时间:1621848350003
I/Work-------->: C开始时间:1621848350132----执行时间:3000----结束时间:1621848353132
I/Work-------->: E开始时间:1621848353314----执行时间:1000----结束时间:1621848354314
image
/**
         A       C
         |       |
         B       D
         |       |
         +-------+
             |
             E
     */
WorkManager workManager = WorkManager.getInstance(MainActivity.this);
WorkContinuation left = workManager.beginWith(requestWorkA).then(requestWorkB);
WorkContinuation right = workManager.beginWith(requestWorkC).then(requestWorkD);
WorkContinuation finalWork = WorkContinuation.combine(Arrays.asList(left, right)).then(requestWorkE);
finalWork.enqueue();
/**
    I/Work-------->: A开始时间:1621934646458----执行时间:1000----结束时间:1621934647458
    I/Work-------->: C开始时间:1621934646459----执行时间:3000----结束时间:1621934649459
    I/Work-------->: B开始时间:1621934647541----执行时间:2001----结束时间:1621934649542
    I/Work-------->: D开始时间:1621934649539----执行时间:1000----结束时间:1621934650539
    I/Work-------->: E开始时间:1621934650703----执行时间:1001----结束时间:1621934651704
*/

数据传递

image
//step 1 
Data myData = new Data.Builder()
                .putString("input", "123")
                .build();
//step 2
new OneTimeWorkRequest.Builder(AWorker.class).setInputData(myData).build();
 //step 3 >AWorker
@NonNull
    @Override
    public Result doWork() {
        String input = getInputData().getString("input");
            Data output = new Data.Builder()
                .putString("output", "456")
                .build();
        return Result.success(output);
    }
//step4
manager.getWorkInfoByIdLiveData(requestWorkA.getId()).observe(activity, new Observer<WorkInfo>() {
            @Override
            public void onChanged(WorkInfo workInfo) {
                if (workInfo != null && workInfo.getState().isFinished()) {
                    try {
                        output = workInfo.getOutputData().getString("output");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    MLogger.i("outPut--->" + output);
                }

            }
        });

input合并器

WorkManager 提供两种不同类型的 InputMerger

总结

引用

上一篇下一篇

猜你喜欢

热点阅读