Rxjava has been in contact for quite a long time, but let me say a why or not come out, in the final analysis or understanding is not deep enough, while the end of this time to write a series out to their own study to make a record
Note the distinction between RxJava1.0 and 2.0, the following default is the test done on the basis of 2.0
To understand several concepts first:
1. Observable: a literal meaning observable, the observer, the person who is the event
2. Observer: The Observer, the recipient of the event
3, subscribe (): The two Create a subscription relationship, it is necessary to note that Observable.subscribe (Observer), it feels like the observer subscribed to the Observer, and the common sense, why so designed? I guess it's for chained calls.
First, the simplest way to use:
1Observable.create (NewObservableonsubscribe<integer>() {2 @Override3 Public voidSubscribe (observableemitter<integer> e)throwsException {4E.onnext (1);5E.onnext (2);6E.onnext (3);7 E.oncomplete ();8 }9}). Subscribe (NewObserver<integer>() {Ten One @Override A Public voidOnsubscribe (disposable d) { -LOG.I (TAG, "Onsubscribe:"); - } the - @Override - Public voidonNext (Integer integer) { -LOG.I (TAG, "OnNext:" +integer); + } - + @Override A Public voidOnError (Throwable e) { atLOG.I (TAG, "OnError:" +e.getmessage ()); - } - - @Override - Public voidOnComplete () { -LOG.I (TAG, "Oncomplete:complete"); in } -});
1, OnNext () can send the event multiple times, OnComplete () sent once, multiple calls will not error, OnError () sent once, multiple calls will be error, not and OnComplete () coexistence
2. After calling OnComplete () or onerror (), the Observer cannot accept the OnNext ()
3, Disposable (2.0 new), when Dispose () is called, the Observer cannot accept the event.
Second, Cold Observable and Hot Observable
Cold Observable: Only when Subscribers subscribe, does the data flow start sending, and each Subscriber subscribes to the data stream (create (), just ().
Hot Observable: If there is no subscriber subscription, once created, start sending data stream
Publish conversions:
1 Connectableobservable<long> ob= observable.interval (timeunit.milliseconds). publish (); converted into cold Observable2 ob.connect (); start sending data stream
If you do not call Connnect (), the data stream is not sent, and once called, a subscription is created and subscribed to the original observable, and the accepted data is forwarded to the Subscriber.
Connect () and disconnect ()
1.0 Connect () returns subscription
2.0 connect () returns disposable
//Note the difference between what you want to release//release S, means interrupt data transfer, reconnect again//release D1 or D2, the representative cancels the registration, the data is already transmitted Public voidDosubscribe (View v) {s=Ob.connect (); //Public final void subscribe (OBSERVER<? Super T> Observer) {} No return value, unable to unregisterD1= Ob.subscribe (NewConsumer<long>() {@Override Public voidAccept (Long along)throwsException {log.i (TAG,"onnext:first============" +along); } }); D2=ob.subscribe (NewConsumer<long>() {@Override Public voidAccept (Long along)throwsException {log.i (TAG,"accept:second==========" +along); } }); }
RefCount
Observable<long> ob= observable.interval (timeunit.milliseconds). Publish (). RefCount ();
If a subscriber sends the data stream, no subscription traffic stops, and the subscription restarts the send (which may be confused with cold observable, note that each Subscriber receives the same data)
Reply
Connectableobservable<long> ob= observable.interval (timeunit.milliseconds). Replay (); Ob.connect ();
After linking with the source Observable, the data collection begins. When there is a Observer subscription, the collected data lines are sent to Observer. And then accept the data with the other Observer.
You can set the number of collected data at the same time
Cache
Observable<long> ob= observable.interval (timeunit.milliseconds). Take (5). cache (); start sending data only when subscribers subscribe
Similar to reply, subscribers will not stop sending after all cancellations.
Rxjava the way to get started (i)