The technical principles behind RXJAVA unveiled
The technical principles behind rxjava revealed the secrets
Rxjava is a library that implements responsive programming on the Java platform.It is widely used in the development of asynchronous event -driven procedures, with simplified and complicated asynchronous operations and event handling.This article will reveal the technical principles behind RXJAVA and how to use its powerful functions to achieve response programming.
1. Observables和Subscribers
The core of RXJAVA is Observables and Subscripers.ObserVables represents a series of data sources that issued events, which can be database queries, network requests, user input, and so on.Subscribers is used to subscribe to Observables, deal with these events and respond.
Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
// Define the events issued by Observables here
emitter.onNext("Hello");
emitter.onNext("World");
emitter.onComplete();
}
});
Subscriber<String> subscriber = new Subscriber<String>() {
@Override
public void onNext(String s) {
// Here
System.out.println(s);
}
@Override
public void onComplete() {
// Here
}
@Override
public void onError(Throwable e) {
// Here to deal with the error encountered
}
};
observable.subscribe(subscriber);
The above code creates a simple Observable, which emits two string events: "Hello" and "World".Subscriber handles and respond to these events by rewriting the corresponding methods.
2. thread scheduling
RXJAVA also provides a wealth of thread scheduling mechanisms for easy operation operations in different threads.Through the SchedUler, you can specify which threads run on Oolvables and Subscripers, and how to switch threads.
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
The above code specifies the Observable's launch event on the IO thread, while the response operation of the Subscriber is performed on the main thread.This can avoid time -consuming operations on the main thread and maintain the smoothness of the interface.
3. Filtering and conversion operators
RXJAVA provides a wealth of events to deal with Observables.By operating the operator, Observables events can be filtered, converted, and merged to achieve complex data processing logic.
Observable.just("Apple", "Banana", "Cherry")
.filter(new Predicate<String>() {
@Override
public boolean test(String s) throws Exception {
// Filter only retains a string with a length greater than 5
return s.length() > 5;
}
})
.map(new Function<String, Integer>() {
@Override
public Integer apply(String s) throws Exception {
// Convert the string to length
return s.length();
}
})
.subscribe(subscriber);
The above code creates an Observable to issue three string events, and use the Filter operator to filter a string with a length of greater than 5, and convert the string to length with the MAP operator.In the end, Subscriber will only receive a string length greater than 5.
Through the combination of these operators, chain data processing logic can be achieved, and the complex event flow can be finely controlled.
4. Error treatment
In response, error treatment is an important link.RXJAVA provides a variety of error processing mechanisms, including processing errors of a single operator, errors in chain operators, and unified global error treatment.
observable.onErrorReturn(new Function<Throwable, String>() {
@Override
public String apply(Throwable throwable) throws Exception {
// When encountering an error, return to silence
return "Error occurred";
}
})
.subscribe(subscriber);
The above code uses the onerRorreturn operator to return a default value to Subscriber when it encounters an error.
Summarize:
RXJAVA is a powerful library that implements responsive programming through the observer mode.Establish event streams through Observables and Subscripers. The rich operator is used to handle these events, while Scheduler provides the flexibility of thread scheduling.These technical principles provide us with a convenient and efficient way to handle asynchronous events.