For details, the principle and practice of RXJAVA
RXJAVA is a powerful response programming library. It is based on the observer mode and uses an observed object and subscriber to achieve asynchronous and event -based procedures.It has an advantage in processing asynchronous operations and processing multiple data streams, and provides a complete set of operators to process data flow.
The principle of rxjava can be summarized as the following key points:
1. Observable: Observation objects represent an event sequence that can issue zero or multiple events.It can be subscribed by the subscriber (Observer). Once a new event is issued, the subscriber can handle the event accordingly.
2. Observer: The subscriber is used to receive events issued by observed objects and can handle the event.It needs to implement the Observer interface and rewrite the corresponding method to define how to handle the event.
3. Subscriber: The observer is a connector between observed objects and subscribers, which can associate the two together.The observer can subscribe to observed objects and send the events received to the subscriber.
4. Operator: The operator is one of the core components of RXJAVA. It can transform and filter the event sequence.The operating symbols can be connected in series to form an operating chain to achieve complex data processing needs.
The practice of rxjava can be divided into the following steps:
1. Add RXJAVA dependencies: First, add the Rxjava library dependency item in your project.You can add the following dependencies in the project construction file (Build.gradle):
dependencies {
implementation 'io.reactivex.rxjava2:rxjava:2.x.x'
}
2. Create Observable: Use Observable.create () to create an observed object and implement its subcribe () method to send events.
Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
emitter.onNext("Hello");
emitter.onNext("RxJava");
emitter.onComplete();
}
});
3. Create Observer: Create a subscriber to receive events issued by Observable.
Observer<String> observer = new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
// The callback when subscribing can be used to cancel the subscription
}
@Override
public void onNext(String s) {
// Treatment the receiving event
}
@Override
public void onError(Throwable e) {
// Treatment errors
}
@Override
public void onComplete() {
// Complete the callback of all events
}
};
4. Subscribe to Observable: Use the Subscrip () method to associate Observable and Observer to achieve the subscription process.
observable.subscribe(observer);
5. Use the operator to process the data: processing and conversion of events issued by Observable through chain call operators.
observable
.map(new Function<String, String>() {
@Override
public String apply(String s) throws Exception {
return s.toUpperCase();
}
})
.filter(new Predicate<String>() {
@Override
public boolean test(String s) throws Exception {
return s.startsWith("R");
}
})
.subscribe(observer);
Through these steps, you can use RXJAVA to achieve responsive programming and process asynchronous operations and multiple data streams.At the same time, RXJAVA also provides rich operators and scheduers to meet various data processing needs.