欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

首頁綜合 正文
目錄

柚子快報激活碼778899分享:Android中的Rxjava

柚子快報激活碼778899分享:Android中的Rxjava

http://yzkb.51969.com/

要使用Rxjava首先要導(dǎo)入兩個包,其中rxandroid是rxjava在android中的擴展

implementation 'io.reactivex:rxandroid:1.2.1'

implementation 'io.reactivex:rxjava:1.2.0'

observer 是一個觀察者接口,泛型T為觀察者觀察數(shù)據(jù)的類型,里面只有三個方法,其中onError()和onCompleted()最后只能調(diào)用其中一個,調(diào)用了此二方法后onNext()將不會在調(diào)用 onNext()方法可以調(diào)用0到多次,觀察到的數(shù)據(jù)處理在此實現(xiàn)。

/**

* Provides a mechanism for receiving push-based notifications.

*

* After an Observer calls an {@link Observable}'s {@link Observable#subscribe subscribe} method, the

* {@code Observable} calls the Observer's {@link #onNext} method to provide notifications. A well-behaved

* {@code Observable} will call an Observer's {@link #onCompleted} method exactly once or the Observer's

* {@link #onError} method exactly once.

*

* @see ReactiveX documentation: Observable

* @param

* the type of item the Observer expects to observe

*/

public interface Observer {

/**

* Notifies the Observer that the {@link Observable} has finished sending push-based notifications.

*

* The {@link Observable} will not call this method if it calls {@link #onError}.

*/

void onCompleted();

/**

* Notifies the Observer that the {@link Observable} has experienced an error condition.

*

* If the {@link Observable} calls this method, it will not thereafter call {@link #onNext} or

* {@link #onCompleted}.

*

* @param e

* the exception encountered by the Observable

*/

void onError(Throwable e);

/**

* Provides the Observer with a new item to observe.

*

* The {@link Observable} may call this method 0 or more times.

*

* The {@code Observable} will not call this method again after it calls either {@link #onCompleted} or

* {@link #onError}.

*

* @param t

* the item emitted by the Observable

*/

void onNext(T t);

}

使用Observer接口創(chuàng)建一個觀察者

Observer observer = new Observer() {

@Override

public void onCompleted() {

ILog.LogDebug("observer onCompleted is come in");

}

@Override

public void onError(Throwable e) {

ILog.LogDebug("observer onError is come in");

}

@Override

public void onNext(String s) {

ILog.LogDebug("observer onNext is come in s= "+s);

}

};

Subscriber 為Observer的一個抽象實現(xiàn)類

public abstract class Subscriber implements Observer, Subscription{

}

使用Subscriber 創(chuàng)建一個觀察者

Subscriber subscriber = new Subscriber() {

@Override

public void onCompleted() {

ILog.LogDebug("subscriber onCompleted is come in");

}

@Override

public void onError(Throwable e) {

ILog.LogDebug("subscriber onError is come in");

}

@Override

public void onNext(String s) {

ILog.LogDebug("subscriber onNext is come in s = "+s);

}

@Override

public void onStart() {

super.onStart();

}

};

其中onStart()方法,它會在事件還未發(fā)送之前被調(diào)用,可以用于做一些準(zhǔn)備工作。例如數(shù)據(jù)的清零或重置。這是一個可選方法,默認情況下它的實現(xiàn)為空。

Observable被觀察者也叫事件發(fā)生源,它決定什么時候觸發(fā)事件以及觸發(fā)怎樣的事件。

public class Observable {

}

Observable 可以使用Observable.create()方法創(chuàng)建

Observable observable = Observable.create(new Observable.OnSubscribe() {

@Override

public void call(Subscriber subscriber) {

subscriber.onNext("observable call onNext0");

subscriber.onStart();

subscriber.onNext("observable call onNext");

subscriber.onCompleted();

subscriber.onNext("observable call onNext1");

}

});

也可以使用just和from方法創(chuàng)建

Observable observable = Observable.just("observable call onNext","observable call onNext1");

String[] array = {"observable call onNext","observable call onNext1"};

Observable observable2 = Observable.from(array);

那么just,from之間的區(qū)別是什么呢,通過查看Observable源碼,just方法內(nèi)部也是調(diào)用的from方法。

public class Observable {

....

public static Observable just(T t1, T t2) {

return from((T[])new Object[] { t1, t2 });

}

....

}

觀察者和被觀察者之間的綁定

observable.subscribe(subscriber);

Rxjava的不完整回調(diào)Action Action后的數(shù)字代表回調(diào)的參數(shù)類型數(shù)量

Action1 onNextAction = new Action1() {

@Override

public void call(String s) {

ILog.LogDebug("onNextAction onNext s = "+s);

}

};

Action2 action2 = new Action2() {

@Override

public void call(String s, String s2) {

}

};

Action1 throwableAction1 = new Action1() {

@Override

public void call(Throwable throwable) {

ILog.LogDebug("throwableAction1 call");

}

};

Action0 onCompleteAction = new Action0() {

@Override

public void call() {

ILog.LogDebug("onCompleteAction is come in");

}

};

//調(diào)用方法

observable.subscribe(onNextAction);

observable.subscribe(onNextAction,throwableAction1);

observable.subscribe(onNextAction,throwableAction1,onCompleteAction);

那么Rxjava的內(nèi)部是怎么使用action的呢 通過源碼可以看到

public class Observable {

......

public final Subscription subscribe(final Action1 onNext, final Action1 onError) {

if (onNext == null) {

throw new IllegalArgumentException("onNext can not be null");

}

if (onError == null) {

throw new IllegalArgumentException("onError can not be null");

}

Action0 onCompleted = Actions.empty();

return subscribe(new ActionSubscriber(onNext, onError, onCompleted));

}

......

public final Subscription subscribe(Subscriber subscriber) {

return Observable.subscribe(subscriber, this);

}

......

}

最后調(diào)用了 subscribe()方法 而 subscribe()方法參數(shù)是Subscriber,通過查看ActionSubscriber源碼,可知還是將action作為參數(shù) 最后轉(zhuǎn)成了Subscriber對象

public final class ActionSubscriber extends Subscriber {

final Action1 onNext;

final Action1 onError;

final Action0 onCompleted;

public ActionSubscriber(Action1 onNext, Action1 onError, Action0 onCompleted) {

this.onNext = onNext;

this.onError = onError;

this.onCompleted = onCompleted;

}

@Override

public void onNext(T t) {

onNext.call(t);

}

@Override

public void onError(Throwable e) {

onError.call(e);

}

@Override

public void onCompleted() {

onCompleted.call();

}

}

同理當(dāng)我們使用Observer接口生成匿名類時,然后再調(diào)用 observable.subscribe(observer);進行綁定也是將Observer轉(zhuǎn)換成Subscriber對象,源代碼如下:

public class Observable {

......

public final Subscription subscribe(final Observer observer) {

if (observer instanceof Subscriber) {

return subscribe((Subscriber)observer);

}

if (observer == null) {

throw new NullPointerException("observer is null");

}

return subscribe(new ObserverSubscriber(observer));

}

......

}

public final class ObserverSubscriber extends Subscriber {

final Observer observer;

public ObserverSubscriber(Observer observer) {

this.observer = observer;

}

@Override

public void onNext(T t) {

observer.onNext(t);

}

@Override

public void onError(Throwable e) {

observer.onError(e);

}

@Override

public void onCompleted() {

observer.onCompleted();

}

}

柚子快報激活碼778899分享:Android中的Rxjava

http://yzkb.51969.com/

好文推薦

評論可見,查看隱藏內(nèi)容

本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。

轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/18563266.html

發(fā)布評論

您暫未設(shè)置收款碼

請在主題配置——文章設(shè)置里上傳

掃描二維碼手機訪問

文章目錄