在0.18中,后台任务如何在调度程序上执行?

sec*_*oot 2 rx-java

感觉就像我在这里遗漏了一些东西,但我曾经在那里做过:

    Schedulers.io().schedule(new Action1<Scheduler.Inner>() {
      @Override
      public void call(Scheduler.Inner inner) {
        doWhatever();
      }
    });
Run Code Online (Sandbox Code Playgroud)

我似乎无法简单地使用调度程序来运行后台任务,而无需管理unsubscribes(https://github.com/Netflix/RxJava/wiki/Schedulerhttps://github.com/Netflix/ RxJava/blob/master/rxjava-core/src/main/java/rx/Scheduler.java).

是否存在0.18的模式,允许我轻松地运行,无需跟踪工人,取消订阅等?

好像你可以这样做:

final Worker worker = Schedulers.io().createWorker();
worker.schedule(new Action0() {
  @Override
  public void call() {
    doWhatever();
    worker.unsubscribe();
  }
});
Run Code Online (Sandbox Code Playgroud)

但这似乎更多的工作,(特别是对于android.mainThread调度程序).

我错过了什么?

Aki*_*ito 13

使用具有Observable的Scheduler

如果您正在使用RxJava,我认为您应该让Observables处理调度程序.在我的代码中,我认为我不必创建自己的工作者并管理它.

将Observable与Scheduler一起使用并在两种线程类型之间切换的示例.

public void doSomething() {
    Observable
            .create(new Observable.OnSubscribe<Boolean>() {
                @Override
                public void call(Subscriber<? super Void> subscriber) {
                    int sleepTime = (int) (Math.random() * 10000);

                    System.out.println("Running on: " + Thread.currentThread().getId() + " sleep: " + sleepTime);

                    try {
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException e) {
                        System.out.println("Error!!! " + Thread.currentThread().getId());
                        subscriber.onError(e);
                        return;
                    }

                    System.out.println("Done!!! " + Thread.currentThread().getId());
                    subscriber.onNext(true);
                    subscriber.onCompleted();
                }
            })
            // this will schedule your work in a background io thread. In this example the "call" method above.
            .subscribeOn(Schedulers.io())
            // this will schedule your next/complete/error on the androids main thread.
            .observeOn(AndroidSchedulers.mainThread())
            // kick off the actual work.
            .subscribe(new Subscriber<Boolean>() {
                @Override
                public void onCompleted() {
                    // runs in main thread.
                }

                @Override
                public void onError(Throwable e) {
                    // runs in main thread.
                }

                @Override
                public void onNext(Boolean result) {
                    // runs in main thread.
                }
            });
}
Run Code Online (Sandbox Code Playgroud)

直接使用Scheduler

但是,我确实理解可能存在需要您直接使用调度程序的情况.所以,如果你想直接使用Scheduler.我认为以下适合您的需求.

使用runAction创建调度程序实用程序.

public static void runAction(Action0 action, Scheduler scheduler) {
    Scheduler.Worker worker = scheduler.createWorker();
    worker.schedule(new Action0() {
        @Override
        public void call() {
            action.call();
            worker.unsubscribe();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

然后使用它,传递要执行的操作和要使用的调度程序.

SchedulerUtil.runAction(new Action0() {
    @Override
    public void call() {
        int sleepTime = (int) (Math.random() * 10000);

        System.out.println("Running on: " + Thread.currentThread().getId() + " sleep: " + sleepTime);
        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException ignored) {
        }

        System.out.println("Done!!! " + Thread.currentThread().getId());
    }
}, Schedulers.io());
Run Code Online (Sandbox Code Playgroud)