RXjava parse (b) I RXjava source and gave this interview you, and you tell me face but can not get the offer?

Just recently I made a video of RXJava of relevant teaching, after finishing the preview information and relevant content and graphics on RxJava and related source code, you need to learn you can contact me ~

To undertake the above: I RXjava source and gave this interview you, and you tell me face but can not get the offer (a)?
Source and interview Daquan PDF
(VX: mm14525201314)
RXjava parse (b) I RXjava source and gave this interview you, and you tell me face but can not get the offer?

RxJava

In RxJavathe, an object that implements the Observer interface may subscribe (Subscribe) Observable instance of a class. Any data or sequence subscriber (Subscriber) to transmit Observable (EMIT) responds. This model simplifies the concurrent operation, because it does not block waiting Observable transmit data, but creates a notification on standby observer sentry, sentries at some point in the future response of Observable.

####Single

Introduction

RxJava (and its derived RxGroovyand RxScala) there is a variant called Single of the Observable. Single similar Observable, the difference is that it always emit only a value, or an error notification, rather than launching a series of values.

Therefore, unlike the method requires three Observable onNext, onError, onCompleted, Single subscription requires only two methods:

  • onSuccess- Single emission value to this method single
  • onError- If you can not transmit the desired value, Single launching a Throwablesubject to this method

Single only after calling one of these two methods, but only one call, a call to any method, subscription relationship termination

Single operator

Single can also combine multiple operations, some of the operators so that you can mix and Observable Single:
RXjava parse (b) I RXjava source and gave this interview you, and you tell me face but can not get the offer?

Subject

Subject可以看成是一个桥梁或者代理,在某些ReactiveX实现中(如RxJava),它同时充当了ObserverObservable的角色。因为它是一个Observer,它可以订阅一个或多个Observable;又因为它是一个Observable,它可以转发它收到(Observe)的数据,也可以发射新的数据。

由于一个Subject订阅一个Observable,它可以触发这个Observable开始发射数据(如果那个Observable是"冷"的--就是说,它等待有订阅才开始发射数据)。因此有这样的效果,Subject可以把原来那个"冷"的Observable变成"热"的。

Subject的种类

针对不同的场景一共有四种类型的Subject。他们并不是在所有的实现中全部都存在,而且一些实现使用其它的命名约定(例如,在RxScala中Subject被称作PublishSubject)。

AsyncSubject

一个AsyncSubject只在原始Observable完成后,发射来自原始Observable的最后一个值。(如果原始Observable没有发射任何值,AsyncObject也不发射任何值)它会把这最后一个值发射给任何后续的观察者。

然而,如果原始的Observable因为发生了错误而终止,AsyncSubject将不会发射任何数据,只是简单的向前传递这个错误通知

BehaviorSubject

当观察者订阅BehaviorSubject时,它开始发射原始Observable最近发射的数据(如果此时还没有收到任何数据,它会发射一个默认值),然后继续发射其它任何来自原始Observable的数据。

然而,如果原始的Observable因为发生了一个错误而终止,BehaviorSubject将不会发射任何数据,只是简单的向前传递这个错误通知

PublishSubject

PublishSubject只会把在订阅发生的时间点之后来自原始Observable的数据发射给观察者。需要注意的是,PublishSubject可能会一创建完成就立刻开始发射数据(除非你可以阻止它发生),因此这里有一个风险:在Subject被创建后到有观察者订阅它之前这个时间段内,一个或多个数据可能会丢失。如果要确保来自原始Observable的所有数据都被分发,你需要这样做:或者使用Create创建那个Observable以便手动给它引入"冷"Observable的行为(当所有观察者都已经订阅时才开始发射数据),或者改用ReplaySubject

如果原始的Observable因为发生了一个错误而终止,PublishSubject将不会发射任何数据,只是简单的向前传递这个错误通知。

ReplaySubject

ReplaySubject会发射所有来自原始Observable的数据给观察者,无论它们是何时订阅的。也有其它版本的ReplaySubject,在重放缓存增长到一定大小的时候或过了一段时间后会丢弃旧的数据(原始Observable发射的)。

如果你把ReplaySubject当作一个观察者使用,注意不要从多个线程中调用它的onNext方法(包括其它的on系列方法),这可能导致同时(非顺序)调用,这会违反Observable协议,给Subject的结果增加了不确定性。

RxJava的对应类

假设你有一个Subject,你想把它传递给其它的代理或者暴露它的Subscriber接口,你可以调用它的asObservable方法,这个方法返回一个Observable。具体使用方法可以参考Javadoc文档。

串行化

如果你把 Subject 当作一个 Subscriber 使用,注意不要从多个线程中调用它的onNext方法(包括其它的on系列方法),这可能导致同时(非顺序)调用,这会违反Observable协议,给Subject的结果增加了不确定性。

要避免此类问题,你可以将 Subject 转换为一个 SerializedSubject,类似于这样:

  mySafeSubject = new SerializedSubject( myUnsafeSubject );

调度器 Scheduler

如果你想给Observable操作符链添加多线程功能,你可以指定操作符(或者特定的Observable)在特定的调度器(Scheduler)上执行。

某些ReactiveX的Observable操作符有一些变体,它们可以接受一个Scheduler参数。这个参数指定操作符将它们的部分或全部任务放在一个特定的调度器上执行。

使用ObserveOnSubscribeOn操作符,你可以让Observable在一个特定的调度器上执行,ObserveOn指示一个Observable在一个特定的调度器上调用观察者的onNext, onErroronCompleted方法,SubscribeOn更进一步,它指示Observable将全部的处理过程(包括发射数据和通知)放在特定的调度器上执行。

RxJava示例

调度器的种类

下表展示了RxJava中可用的调度器种类:
RXjava parse (b) I RXjava source and gave this interview you, and you tell me face but can not get the offer?

默认调度器

在RxJava中,某些Observable操作符的变体允许你设置用于操作执行的调度器,其它的则不在任何特定的调度器上执行,或者在一个指定的默认调度器上执行。下面的表格个列出了一些操作符的默认调度器:
RXjava parse (b) I RXjava source and gave this interview you, and you tell me face but can not get the offer?

使用调度器

除了将这些调度器传递给RxJavaObservable操作符,你也可以用它们调度你自己的任务。下面的示例展示了Scheduler.Worker的用法:

  worker = Schedulers.newThread().createWorker();
  worker.schedule(new Action0() {
    @Override
    public void call() {
      yourWork();
   }
  });
  // some time later...
  worker.unsubscribe();

递归调度器

To schedule a recursive call, you can use schedule, then use schedule(this)the example:

  worker = Schedulers.newThread().createWorker();
  worker.schedule(new Action0() {
    @Override
    public void call() {
      yourWork();
      // recurse until unsubscribed (schedule will do nothing if unsubscribed)
      worker.schedule(this);
   }
  });
  // some time later...
  worker.unsubscribe();

Check the settings or unsubscribe state

Object Worker Subscription class implements an interface, and use it isUnsubscribed unsubscribe method, so you can stop the task to cancel your subscription or unsubscribe, examples from internal tasks are scheduled:

  Worker worker = Schedulers.newThread().createWorker();
  Subscription mySubscription = worker.schedule(new Action0() {

       @Override
       public void call() {
           while(!worker.isUnsubscribed()) {
               status = yourWork();
               if(QUIT == status) { worker.unsubscribe(); }

           }
       }
  });

WorkerSimultaneously Subscription, so you can (and often should) call its unsubscribemethods can notice the pending tasks and free up resources.

And the delay period scheduler

You can use the schedule(action,delayTime,timeUnit)delay you perform tasks specified in the scheduler, the following example tasks will begin after 500 msec:

  someScheduler.schedule(someAction, 500, TimeUnit.MILLISECONDS);

Use another version of the schedule,schedulePeriodically(action,initialDelay,period,timeUnit)method so that you can schedule a task to perform on a regular basis, examples of the following tasks will be performed after 500 milliseconds, and then executed every 250 milliseconds:

  someScheduler.schedulePeriodically(someAction, 500, 250, TimeUnit.MILLISECONDS);

Test scheduler

TestScheduler so that you can manually fine-tune the performance of the scheduler clock. This test relies on the precise timing of tasks useful. The scheduler has three additional methods:

  • advanceTimeTo(time,unit) Clock forward fluctuation scheduler to a specified point in time
  • advanceTimeBy(time,unit) The scheduler clock forward toggle a specified period of time
  • triggerActions( )Before you begin any task planned but not started, if their plans as early as the clock time is equal to or scheduler current time

( Pulled left GitHub link, you need access to relevant content such as interviews can find their own )
https://github.com/xiangjiana/Android-MS
(VX:mm14525201314)

Guess you like

Origin blog.51cto.com/14541311/2465363