1. Subject
Point open RxJava2 Documentation , find a description of the Subject:
public abstract class Subject<T>
extends Observable<T>
implements Observer<T>
For Observable, Observer, Subscriber we are familiar with, so don't explain it, focus on Subject.
From the diagram above, we can see that Subject inherits from Observable, which means Subject can be an observer, and it implements the Observer interface, which means it can also be an observer.It is not difficult to see that Subject can be subscribed to Observable as an Observer and to other Observers as an Observable.In summary, Subject assumes the role of observer above and observer below.
Classification of Subject s
As we can see from the uml above, RxJava provides us with four commonly used Subject s, namely
AsyncSubject,BehabviorSubject,PublishSubject,ReplaySubject, which are described below.
1.AsyncSubject
An Subject that emits the very last value followed by a completion event or the received error to Observers.
AsyncSubject caches the last data and sends it to the subscriber when onCompleted() is called. In the process, if any exception occurs, the data is not sent to the subscriber, but an exception notification is sent to the subscriber, that is, the subscriber can only receive one exception notification.
Example usage of AsyncSubject:
asyncSubject.onNext("1");
asyncSubject.onNext("2");
asyncSubject.onCompleted();//Must be called to start sending data
After the above code is executed, the data that subscribers receive is 2.
2.BehabviorSubject
Subject that emits the most recent item it has observed and all subsequent observed items to each subscribed Observer.
When a BehaviorSubject is subscribed, it first sends the data that the original Observable has recently sent, and if it hasn't recently, it sends a default value, then continues to send the original Observable's data.
Examples are as follows:
// observer will receive all 4 events (including "default").
BehaviorSubject<Object> subject = BehaviorSubject.createDefault("default");
subject.subscribe(observer);
subject.onNext("one");
subject.onNext("two");
subject.onNext("three");
// observer will receive the "one", "two" and "three" events, but not "zero"
BehaviorSubject<Object> subject = BehaviorSubject.create();
subject.onNext("zero");
subject.onNext("one");
subject.subscribe(observer);
subject.onNext("two");
subject.onNext("three");
// observer will receive only onComplete
BehaviorSubject<Object> subject = BehaviorSubject.create();
subject.onNext("zero");
subject.onNext("one");
subject.onComplete();
subject.subscribe(observer);
// observer will receive only onError
BehaviorSubject<Object> subject = BehaviorSubject.create();
subject.onNext("zero");
subject.onNext("one");
subject.onError(new RuntimeException("error"));
subject.subscribe(observer);
3.PublishSubject
Subject that, once an Observer has subscribed, emits all subsequently observed items to the subscriber.
Observable starts sending events as soon as it is subscribed, as shown in the following figure:
Example:
PublishSubject<Object> subject = PublishSubject.create();
// observer1 will receive all onNext and onComplete events
subject.subscribe(observer1);
subject.onNext("one");
subject.onNext("two");
// observer2 will only receive "three" and onComplete
subject.subscribe(observer2);
subject.onNext("three");
subject.onComplete();
4.ReplaySubject
Replays events to Observers.
ReplaySubject caches all sent data, and when a new subscription relationship is generated, ReplaySuject sends all data to him.As follows:
ReplaySubject<Object> subject = new ReplaySubject<>();
subject.onNext("one");
subject.onNext("two");
subject.onNext("three");
subject.onComplete();
// both of the following will get the onNext/onComplete calls from above
subject.subscribe(observer1);
subject.subscribe(observer2);
Summary
Subject | |
---|---|
AsyncSubject | No matter when the subscription occurs, only the last data will be sent |
BehaviorSubject | Send the data before the subscription and all the data after the subscription |
ReplaySubject | Emit all data whenever a subscription occurs |
PublishSubject | All data after sending subscription |
2. Single
Single is similar to Observable, except that it always emits only one value, or an error notification, rather than a series of values.
Therefore, unlike Observable, which requires three methods onNext, onError, and onCompleted, subscribing to Single requires only two methods:
onSuccess - Single emits a single value to this method
onError - Single emits a Throwable object to this method if the required value cannot be emitted
Single calls only one of these two methods, and only once. After calling either method, the subscription relationship terminates.
Simple example
Single.just(addValue(1, 2)).subscribe(new SingleSubscriber<Integer>() {
@Override
public void onSuccess(Integer value) {
// value = 3
}
@Override
public void onError(Throwable error) {}
});
Single can also be mixed with the following operators
Operator | Return value | Explain |
---|---|---|
compose | Single | Create a custom operator |
concat and concatWith | Observable | Connect data sent by multiple Single s and Observable s |
create | Single | Call the observer's create method to create a Single |
error | Single | Returns a Single that immediately sends error notifications to subscribers |
flatMap | Single | Returns a Single that emits the result of a flatMap operation on the original Single's data |
flatMapObservable | Observable | Returns an Observable that emits the results of a flatMap operation on the original Single's data |
from | Single | Convert Future to Single |
just | Single | Returns a Single that transmits a specified value |
map | Single | Returns a Single that emits the result of a map operation on the original Single's data |
merge | Single | Converts one Single (which emits data from another Single, assuming B) to another Single (which emits data from another Single(B) |
merge and mergeWith | Observable | Combining data from multiple Single s |
observeOn | Single | Indicates how Single calls subscribers on the specified dispatcher |
onErrorReturn | Single | Converts a Single that transmits error notifications to a Single that transmits the specified data item |
subscribeOn | Single | Indicates that Single performs operations on the specified dispatcher |
timeout | Single | It adds timeout control to the original Single and sends an error notification if it does |
toSingle | Single | Converts an Observable that transmits a single value to a Single |
zip and zipWith | Single | Converting multiple Single s into one, which emits data as a result of applying a function to the former |