本文CSDN地址:csdn
自從去年開始接觸RxJava,深深的被rxjava的設計吸引,粗略的閱讀了RxJava1的原始碼之後,感觸頗深,今年年初專案更是轉用了RxJava2,對RxJava這種函數語言程式設計更加感興趣,不過本文的重點不在於原始碼解析,如果想看原始碼解析,可以出門到這裡-->:zxt0601
如果你上文已經看完了,那麼我們不難發現,其實RxJava本身是觀察者模式,通過Observable訂閱者subscribe方法訂閱observer觀察者,我們首先看一下Observable中的create方法:
public static <T> Observable<T> create(ObservableOnSubscribe<T> source) {
ObjectHelper.requireNonNull(source, "source is null");
return RxJavaPlugins.onAssembly(new ObservableCreate<T>(source));
}複製程式碼
首先我們先看ObservableOnSubscribe引數,這是一個介面,內部實現也很簡單:
public interface ObservableOnSubscribe<T> {
/**
* Called for each Observer that subscribes.
* @param e the safe emitter instance, never null
* @throws Exception on error
*/
void subscribe(@NonNull ObservableEmitter<T> e) throws Exception;
}複製程式碼
而ObservableEmitter也是一個介面類,而他的作用就比較明顯了,我們這裡主要看一個ObservableEmitter的父類Emitter:
public interface Emitter<T> {
/**
* Signal a normal value.
* @param value the value to signal, not null
*/
void onNext(@NonNull T value);
/**
* Signal a Throwable exception.
* @param error the Throwable to signal, not null
*/
void onError(@NonNull Throwable error);
/**
* Signal a completion.
*/
void onComplete();
}複製程式碼
到這裡可能很多人會一頭霧水,摸不著頭腦,接下來我們找到ObservableCreate這個類中,就能很直觀的看到其呼叫關係:
public final class ObservableCreate<T> extends Observable<T> {
final ObservableOnSubscribe<T> source;
public ObservableCreate(ObservableOnSubscribe<T> source) {
this.source = source;
}
@Override
protected void subscribeActual(Observer<? super T> observer) {
CreateEmitter<T> parent = new CreateEmitter<T>(observer);
observer.onSubscribe(parent);
try {
source.subscribe(parent);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
parent.onError(ex);
}
}複製程式碼
我們知道Observable在進行subscribe的時候真正內部呼叫的是抽象方法subscribeActual,這裡我們很清晰的看到 observer.onSubscribe(parent)與source.subscribe(parent)兩個關鍵的方法,前者是observer是否disposed,而後者則是實現了ObservableOnSubscribe介面的方法,當我們呼叫subscribe,便會呼叫這兩個方法,那麼我們回過頭來看CreateEmitter,這個類內部實現了ObservableEmitter與Disposable兩個介面,對於我們來說比較有用的可能就是以下的程式碼-->
final Observer<? super T> observer;
CreateEmitter(Observer<? super T> observer) {
this.observer = observer;
}
@Override
public void onNext(T t) {
if (t == null) {
onError(new NullPointerException("onNext called with null. Null values are generally not allowed in 2.x operators and sources."));
return;
}
i
f (!isDisposed()) {
observer.onNext(t);
}
}
@Override
public void onError(Throwable t) {
if (!tryOnError(t)) {
RxJavaPlugins.onError(t);
}
}
@Override
public boolean tryOnError(Throwable t) {
if (t == null) {
t = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
}
if (!isDisposed()) {
try {
observer.onError(t);
} finally {
dispose();
}
return true;
}
return false;
}
@Override
public void onComplete() {
if (!isDisposed()) {
try {
observer.onComplete();
} finally {
dispose();
}
}
}
@Override
public void setDisposable(Disposable d) {
DisposableHelper.set(this, d);
}
@Override
public void setCancellable(Cancellable c) {
setDisposable(new CancellableDisposable(c));
}
@Override
public void dispose() {
DisposableHelper.dispose(this);
}
@Override
public boolean isDisposed() {
return DisposableHelper.isDisposed(get());
}複製程式碼
看到這裡各位看官大佬可能就比較明白了,當我們呼叫source.subscribe(parent)內部parent介面的各個方法時,其實最終還是呼叫了observer的的方法。
到這裡之後,其實不難發現,如果我們想要對第三方的api進行封裝,完成rxjava式的封裝,也是輕而易舉的。今天首先我們先嚐試對高德地圖的Api進行封裝,首先這裡是demo地址-->github
首先是高德官方定位的demo,請往這裡看-->高德定位
我們通過demo分析,我們最終需要呼叫的是AMapLocation物件,而AMapLocation物件是在介面AMapLocationListener中進行呼叫,那麼我們首先定義一個Observable如下:
public class LocationObservable extends Observable<AMapLocation> {
private final AMapLocationClient aMapLocationClient;
public LocationObservable(AMapLocationClient aMapLocationClient) {
this.aMapLocationClient = aMapLocationClient;
}
@Override
protected void subscribeActual(Observer<? super AMapLocation> observer) {
AMapCallBack callBack=new AMapCallBack(aMapLocationClient,observer);
aMapLocationClient.setLocationListener(callBack);
observer.onSubscribe(callBack);
aMapLocationClient.startLocation();
}
}複製程式碼
這裡基本就是對ObservableCreate類的改造,通過AMapLocationClient.setLocationListener進行註冊監聽,而AMapCallBack中對observer進行onNext、onComplete、onError的方法呼叫:
private static final class AMapCallBack implements AMapLocationListener, Disposable {
private boolean isDisposed = false;
private final AMapLocationClient aMapLocationClient;
private final Observer<? super AMapLocation> observer;
public AMapCallBack(AMapLocationClient aMapLocationClient, Observer<? super AMapLocation> observer) {
this.aMapLocationClient = aMapLocationClient;
this.observer = observer;
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
try {
if (aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
observer.onNext(aMapLocation);
if(isDisposed){
observer.onComplete();
}
}else {
try {
observer.onError(new AMapLocationException(aMapLocation.getErrorCode(),aMapLocation.getErrorInfo()));
} catch (Throwable inner) {
Exceptions.throwIfFatal(inner);
RxJavaPlugins.onError(new CompositeException(new AMapLocationException(aMapLocation.getErrorCode(),aMapLocation.getErrorInfo()), inner));
}
}
}
}catch (Throwable t){
if(isDisposed){
RxJavaPlugins.onError(t);
}else {
try {
observer.onError(t);
} catch (Throwable inner) {
Exceptions.throwIfFatal(inner);
RxJavaPlugins.onError(new CompositeException(t, inner));
}
}
}
}
@Override
public void dispose() {
aMapLocationClient.onDestroy();
isDisposed = true;
}
@Override
public boolean isDisposed() {
return isDisposed;
}
}複製程式碼
這樣我們就完成了對高德定位LocationObservable的封裝,那麼我們該如何呼叫呢,首先我們需要一個外觀類,命名為AMapRxHelper,然後對需求進行分析,因為高德sdk很多處地方都會用到applicationContext,如果我們每次呼叫都進行傳參,會比較麻煩,所以我們通過全域性初始化applicationContext,在application中呼叫。
private static Application app;
public static void init(Application application) {
app = application;
}複製程式碼
而定位所需要的AMapLocationClient需要很多的初始化方法,我們應交由呼叫者自己實現,那麼該怎麼辦呢,我們不妨參照RxJava中map操作符的實現,通過一個transfer的介面實現:
public static Observable<AMapLocation> createAMapLocation(final LocationSettingsListener listener) {
AMapLocationClient client = new AMapLocationClient(app);
return Observable.just(client)
.flatMap(new Function<AMapLocationClient, ObservableSource<AMapLocation>>() {
@Override
public ObservableSource<AMapLocation> apply(@NonNull AMapLocationClient client) throws Exception {
if (listener != null) {
listener.locationOptions(client);
}
return new LocationObservable(client);
}
});
}
public interface LocationSettingsListener {
void locationOptions(AMapLocationClient client);
}複製程式碼
這樣的話我們的定位封裝就已經完成了,我們在程式碼中實驗一波,activity程式碼如下:
AMapRxHelper.createAMapLocation(new AMapRxHelper.LocationSettingsListener() {
@Override
public void locationOptions(AMapLocationClient client) {
AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
mLocationOption.setOnceLocation(true);
mLocationOption.setOnceLocationLatest(true);
client.setLocationOption(mLocationOption);
}
}).subscribe(new Consumer<AMapLocation>() {
@Override
public void accept(AMapLocation aMapLocation) throws Exception {
Toast.makeText(MainActivity.this,aMapLocation.toStr(),Toast.LENGTH_SHORT).show();
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Toast.makeText(MainActivity.this,throwable.getMessage(),Toast.LENGTH_SHORT).show();
}
});複製程式碼
這樣寫下來是不是要比高德原生的api更加舒服而且函式式呢?當然這只是其中一個例子,在原始碼的demo中還分別實現了poi搜尋,輸入關鍵字搜尋,繫結EditText的關鍵字搜尋,有興趣的小夥伴可以去看下,順便能給個star就更好了~