What is RxJava?
is for Event using observable sequences.
In a nutshell, RxJava is an event-based class library that uses an asynchronous response extension that can be observed in a sequence.
This sentence has the following meanings:
RxJava is a solution to asynchronous problems.
RxJava is based on the event mechanism.
RxJava is the response of an event that is handled by a response extension, such as which thread is responding to an event? )。
The RxJava are sequential. It is a sequential queue, in order, advanced. It supports the collection object very well.
RxJava is implemented using the observer pattern in the design pattern.
Referencing a class library
' io.reactivex:rxjava:1.0.14 ' ' io.reactivex:rxandroid:1.0.1'
How to use it in codeRxjava's code looks like this:
Observable. from(Userbeans). Map (NewFunc1<userbean, string>() {@Override PublicString Call (UserBean UserBean) {returnString.Format ("%s (%s)", Userbean.name, userbean.age); }}). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()) . Subscribe (NewAction1<string>() {@Override Public voidCall (String str) {alert (str); } });
This piece of code tells us:
Observable.from () Creates an object that is being observed.
The. Map () method performs a conversion to convert an object to a string
. Subscribeon () indicates in which thread the above conversion operation is performed.
. Observeon () indicates in which thread the response to the result is performed, such as the following alert method.
The. Subscribe () passes the specific response processing, that is, the alert method is executed.
The code is elegant, chained, and clearly expresses the behavior of the code.
Create a demonstration of a observed object from a single object
Use the Just () method to specify a single object to create the observed object instance.
Private void Dosomeone () { observable.just ("Hello, world!"). Subscribe (new action1<string>() { @Override publicvoid Call (String str) { alert (str);}} );
Create a demonstration of a observed object from a collection
Use the From () method to specify a collection to create the observed object instance.
Private void Dosomearray () { observable.from(new string[]{"A", "B", "C"}). Subscribe (new Action1 <String>() { @Override publicvoid call (String str) { alert ( (str);}} ); }
Presentation of object conversions
We use the map () method to convert. The following code demonstrates creating a observed object from an Userbean array and then converting it to a string using the map () method.
Private voiddosomeentity () {userbean[] Userbeans=NewUserbean[3]; userbeans[0] =NewUserBean ("Jo", 18); userbeans[1] =NewUserBean ("Ken", 9); userbeans[2] =NewUserBean ("hack", 30); //added map method, go out entity to stringObservable.from (Userbeans). Map (NewFunc1<userbean, string>() {@Override PublicString Call (UserBean UserBean) {returnString.Format ("%s (%s)", Userbean.name, userbean.age); }}). Subscribe (NewAction1<string>() {@Override Public voidCall (String str) {alert (str); } }); }
Reference:
Https://github.com/ReactiveX/RxJava
http://gank.io/post/560e15be2dca930e00da1083
Get started with Rxjava on Android