The operator RxJava defer

https://blog.csdn.net/zhuxuliao/article/details/51542809

Defer operators
1. Role
defer operators and create, just, from such as operator, the operator of the class is created, but all related to the operator in the data are only subscription is in force.

2. Usage:
    I = 12 is;
    // parameters are noted here defer Func0, instead OnSubscrie
    Observable Observable = Observable.defer (new new func0 <Observable <>> Integer () {
        @Override
        // call method noted herein no Subscriber parameters
        public Observable <Integer> Call () {
            return new new Observable.from (I);
        }
    });

    i = 15;

    observable.subscribe (the Action1 new new <Integer> () {
        @Override
        public void Call (Integer I) {
            System.out.println ( "Integer = [" + Integer + "]");
        }
    })
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
the output of the code is: integer = [15]

3. Event flow analysis
related categories: OnSubscribeDefer, the specific process is as follows: 
1. Generate Observable objects; pay attention to defer the argument here is Func0, after a call to defer, to continue to speak Func0 as parameters to create ObSubscribeDefer instance.

    Observable.defer (new new func0 <Observable <>> Integer () {
        @Override
        // note here Subscriber call parameter method is not
        public Observable <Integer> call () {
            return new new Observable.from (I);
        }
    });
. 1
2
. 3
. 4
. 5
. 6
. 7
2. Feed; when a subscription relationship, will invoke the call method in example OnSubscribeDefer. 1, and then call a method func0 call in the call method, all the data used is Observable in recent calls from local subscribe values.

    observable.subscribe (the Action1 new new <Integer> () {
        public void Call (for Subscriber Subscriber) {
            // do Something
        }
    }
. 1
2
. 3
. 4
. 5
summarizes: defer operator is relatively simple, as the examples of Func1 OnSubscribeDefer parameters to build the object, ; in the event of subscription relationship, go to a callback func1.call ()
thinking
(after or future get callable data) regardless of the basic create or just, from or defer, finished at the end of the call method OnSubscribe (or Observer, Subscriber) of It is how to evoke call the method Observer, complete method onError even approach it?

create operation after the end of the call method, manually call subscriber.next () or subscriber.complete () method.
just, from or defer are called in the producer in onComplete, onNext, onError method.
future, ObSubscribeToObservableFuture.call first method, in this method, when the future callable taken to the return value of the callback onNext, onComplete, OnError method.
 

Guess you like

Origin blog.csdn.net/lppl010_/article/details/91364521