What is a rxJava?
RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-based programs by using observable sequences. In simple terms, rxJava is an event-based library with asynchronous response extensions for observable sequences.Copy the code
features
- RxJava addresses asynchrony.
- RxJava is event-based.
- RxJava handles response extensions, addressing the response issues of events (such as which thread is the response to an event?). .
- RxJava is sequential. It’s a sequential queue, sequential, first in, last out. It has good support for collection objects.
RxJava uses the Observer pattern in design pattern for implementation. Its core ideas are two things:
- Observed The object being observed, which is an event source whose state will be observed by the subscriber.
- The observer (subscriber) focuses on the object being observed
- Subscription establishes a relationship. We say that the subscriber subscribes to the observed.
RxJava can be used to improve the user experience by easily switching the thread (UI thread or worker thread) on which code is running. Similar to AsyncTask, rxJava allows us to execute time-consuming logic in the worker thread, and then handle the view state numbering in the UI thread. Rxjava brings readability and clarity to code.
Reference library
The compile 'IO. Reactivex: rxjava: 1.0.14' compile 'IO. Reactivex: rxandroid: 1.0.1'Copy the code
A simple example
Rxjava code is elegant, chained, and clearly expresses the behavior of the code.
Observable.from () creates an Observable object. The.map() method performs the transformation, converting the object to a string. SubscribeOn () indicates which thread to execute the transformation above. .observeon () indicates the thread in which the response to the result is performed, as in the alert method below. .subscribe() transmits the concrete response processing, that is, the alert method.
Example:
Observable.from(userBeans) .map(new Func1<UserBean, String>() {@override public String call(UserBean UserBean) {return string. format("%s(%s)", userbean.name, userBean.age); } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<String>() { @Override public void call(String str) { alert(str); // Display in UI view}});Copy the code
A demonstration of creating observed objects from a single object
Using the just() method, specify a single object to create an instance of the observed object.
private void doSomeOne() {
Observable.just("Hello, world!").subscribe(new Action1<String>() {
@Override
public void call(String str) {
alert(str);
}
});
}
Copy the code
Create a demo of the observed object from a collection
Using the from() method, specify a collection to create instances of the observed objects.
private void doSomeArray() { Observable.from(new String[]{"A", "B", "C"}).subscribe(new Action1<String>() { @Override public void call(String str) { alert(str); }}); }Copy the code
Object conversion demo
We use the map() method for the transformation. The following code demonstrates creating an observed object from a UserBean array and converting it to a string using the map() method.
private void doSomeEntity() { UserBean[] userBeans = new UserBean[3]; userBeans[0] = new UserBean("jo", 18); userBeans[1] = new UserBean("ken", 9); userBeans[2] = new UserBean("hack", 30); // Add map method, Transform the entity to the string Observable. From (userBeans). Map (new Func1<UserBean, String>() { @Override public String call(UserBean userBean) { return String.format("%s(%s)", userBean.name, userBean.age); } }).subscribe(new Action1<String>() { @Override public void call(String str) { alert(str); }}); }Copy the code
The Scheduler Scheduler
Scheduler is used to specify which thread to run on. Such as:
SubscribeOn (Schedulers. IO ()) directive in IO thread running. ObserveOn (AndroidSchedulers. MainThread ()) instructions to run in the main threadCopy the code
SubscribeOn method
It specifies that subscribe() occurs on the IO thread. In general, we commonly use this method to process images, network access, which is assigned to the IO thread.
observeOn
It specifies the callback occurred in the main thread of the Subscriber. ObserveOn (AndroidSchedulers. MainThread ()) in general, under the android will use the main thread to operational view
Scheduler has the following options:
- Schedulers.immediate() runs on the current thread, which is equivalent to not specifying a thread. This is the default Scheduler.
- Schedulers.newthread () always starts a newThread and performs operations on the newThread.
- Schedulers.io() The Scheduler used for I/O operations (reading and writing to files, reading and writing to databases, network information interaction, and so on). The behavior of IO () is similar to that of newThread(), except that the internal implementation of IO () uses an unlimited pool of threads and can reuse idle threads, so in most cases IO () is more efficient than newThread(). Do not put “computational work” in IO () to avoid creating unnecessary threads.
- Scheduler putation() The Scheduler used in the calculation. This calculation refers to CPU intensive computing, that is, operations that do not limit performance by operations such as I/O, such as graphics computation. This Scheduler uses a fixed thread pool of CPU cores. I/O operations need to computation(), or WAIT time for I/O operations will waste CPU.
- AndroidSchedulers. MainThread () under the Android characteristic, the Android run the main thread.
Reference:
Github.com/ReactiveX/R… Gank. IO/post / 560 e15…