日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

java if or android_RxJava switchIfEmpty操作符实现Android检查本地缓存逻辑判断

發布時間:2023/12/4 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java if or android_RxJava switchIfEmpty操作符实现Android检查本地缓存逻辑判断 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

switchIfEmpty(Observable emptyObservable)操作符從字面意思上就很好理解,就是當為空的時候跳轉到emptyObservable。

那末如何理解當為空的時候. 下面將會使用實際案例解釋這個switchIfEmpty的使用方法。

業務需求

假設我們的app里有加載文章列表功能,要求加載的邏輯以下:加載文章的的時候,先從本地加載,如果本地存在就是用本地的數據,如果不存在從網絡獲得。

下面是業務代碼:

//從本地數據獲得文章列表

getArticlesObservable(pageIndex, pageSize, categoryId)

//本地不存在,要求api

.switchIfEmpty(articleApi.getArticlesByCategoryId(pageIndex + "", pageSize + "", categoryId + "")

.compose(this.handlerResult())

.flatMap(new Func1>() {

@Override

public Observable call(RespArticlePaginate respArticlePaginate) {

if (respArticlePaginate != null && respArticlePaginate.getList() != null) {

try {

articleDao.insertOrReplaceInTx(respArticlePaginate.getList());

} catch (Exception e) {

e.printStackTrace();

}

}

return Observable.just(respArticlePaginate);

}

}))

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(createSubscriber(ID_ARTICLE_LIST)))

這里的 createSubscriber 封裝了Subscriber對成功、失敗的數據處理,然后統1把數據推給上1層,就不用每一個地方都寫下面相同的模板代碼了:

observable.subscribe(new Action1() {

@Override

public void call(RespArticlePaginate respArticlePaginate) {

//success data

}

}, new Action1() {

@Override

public void call(Throwable throwable) {

// error data

}

})

那末createSubscriber是如何實現的,先看subscribe方法源碼 以下:

public final Subscription subscribe(final Action1 super T> 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));

}

很簡單,他是直接new了1個ActionSubscriber,然后把我們之前在代碼里寫的各個回調(onNext、onError、onComplete)當作參數傳遞進去。那末我們的createSubscriber也能夠摹擬它的實現:

/**

* 處理結果(分發結果) 封裝

*

*@param id 辨別業務類型

*/

protected ActionSubscriber createSubscriber(final int id) {

//由于我們只關心onNext和onError

Action0 onCompleted = Actions.empty();

return new ActionSubscriber(new Action1() {

@Override

public void call(T t) {

pushSuccessData(id, t);

}

}, new Action1() {

@Override

public void call(Throwable throwable) {

pushThrowable(id, throwable);

}

}, onCompleted);

}

好了,言歸正傳,回到我們上面提到的需求。根據需求我們來分析下代碼:

getArticlesObservable方法用來從本地獲得文章列表,articleApi.getArticlesByCategoryId方法是用來當本地不存在的時候從網絡獲得。仿佛這些代碼可以實現了我們上面提到的需求了。而且很簡潔。

實踐是檢驗真諦的唯1標準,我們先運行下看看(本地環境是數據庫沒有文章列表)。

運行后,發現界面并沒有展現數據,通過debug返現,代碼履行了檢測本地緩存的邏輯,且本地找不到符合邏輯的數據,也就是說從本地找到的結果為空。但是沒有依照我們料想的是履行網絡要求。

先來看看查詢本地緩存的代碼是是甚么模樣。

Observable.create(new Observable.OnSubscribe() {

@Override

public void call(Subscriber super Object> subscriber) {

try {

List as = articleDao.queryBuilder()

.where(ArticleDao.Properties.CategoryId.eq(categoryId))

.orderDesc(ArticleDao.Properties.Id)

.offset((pageIndex - 1) * pageSize)

.limit(pageSize).list();

if (as == null || as.isEmpty()) {

subscriber.onNext(null);

}else{

subscriber.onNext(as);

}

}catch (Exception e){

subscriber.onError(e);

}

subscriber.onCompleted();

}

});

通過debug發現代碼走的邏輯是

if (as == null || as.isEmpty()) {

subscriber.onNext(null);

}

發送的是空,為何還是沒有走switchIfEmpty里的邏輯呢?肯定是我們用的姿式不對,先看看該該方法的說明:

/**

* Returns an Observable that emits the items emitted by the source Observable or the items of an alternate

* Observable if the source Observable is empty.

*

*

*

Scheduler:

*

{@code switchIfEmpty} does not operate by default on a particular {@link Scheduler}.

*

*

* @param alternate

* the alternate Observable to subscribe to if the source does not emit any items

* @return an Observable that emits the items emitted by the source Observable or the items of an

* alternate Observable if the source Observable is empty.

* @since 1.1.0

*/

public final Observable switchIfEmpty(Observable extends T> alternate) {

return lift(new OperatorSwitchIfEmpty(alternate));

}

重點關注對參數Observable extends T> alternate的解釋:

the alternate Observable to subscribe to if the source does not emit any items

意思是如果原來的Observable沒有發射任何數據(emit any items),則使用alternate代替原來的Observable。

好,再看看我們的代碼邏輯:

if (as == null || as.isEmpty()) {

subscriber.onNext(null);

}

這段代碼不是沒有發射數據,而是發射了個空數據,也就是發射了null,所以這段代碼其實不是沒有發射任何數據,所以為何不走網絡要求的邏輯。

知道緣由就好解決了,加上個過濾就能夠解決問題了:

.filter(new Func1() {

@Override

public Boolean call(RespArticlePaginate respArticlePaginate) {

return respArticlePaginate != null;

}

})

總結

1,通過switchIfEmpty可以做到1些邏輯判斷,固然實現類型的判斷本地緩存的,可以通過concat結合takeFirst操作符來實現,具體的可以看我之前的博客文章

2,上面通過Observable.create方式來包裝數據查詢,不是很優雅。下1篇博客介紹如何封裝RxJava,使得我們的代碼支持RxJava鏈式調用。

總結

以上是生活随笔為你收集整理的java if or android_RxJava switchIfEmpty操作符实现Android检查本地缓存逻辑判断的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。