z-chu / RxCache

简单一步,缓存搞定。这是一个专用于 RxJava,解决 Android 中对任何 Observable 发出的结果做缓存处理的框架

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

为什么本地有缓存数据,但是没有网络的情况下,仍然加载不了缓存数据,而是报的网络异常错误,是哪里被拦截了吗?

HuaZaiWuDi opened this issue · comments

``//这是配置
public Observable doNetSubscribe(Observable observable,
BehaviorSubject lifecycleSubject,
String cacheKey,
IObservableStrategy strategy
) {
return observable
.compose(RxThreadUtils.bindLife(lifecycleSubject))
.compose(rxCache.transformObservable(cacheKey, String.class, strategy))
.map(new Function<CacheResult, T>() {
@OverRide
public T apply(CacheResult tCacheResult) throws Exception {
Log.e("本地缓存数据:", tCacheResult.toString());
return tCacheResult.getData();
}
})
// .map(new CacheResult.MapFunc())
.compose(RxThreadUtils.handleResult())
.compose(RxThreadUtils.rxThreadHelper());
}
//这是网络请求
RxManager.getInstance().doNetSubscribe(dxyService.indexInfo(1, 7),
lifecycleSubject, "indexInfo", CacheStrategy.cacheAndRemote())
.subscribe(new RxNetSubscriber() {
@OverRide
protected void _onNext(String s) {
RxLogUtils.d("加载数据:" + s);
FirstPageBean bean = MyAPP.getGson().fromJson(s, FirstPageBean.class);
notifyData(bean);
}

                @Override
                protected void _onError(String error, int code) {
                    RxLogUtils.e("异常:" + error);
                    super._onError(error, code);
                    RxToast.normal(error);
                }
            });`

cacheAndRemote 策略加载完缓存之后依然会去加载网络,如果有缓存但加载网络失败会依次调用onNext()、onError()

尝试使用FirstCache来实现你的需求,如果缓存中有数据则不会再加载网络

这个问题确实存在。使用先加载缓存在加载网络的时候。本地有缓存。但是没有加载本地缓存,直接报了网络异常

 Observable<CacheResult<T>> cache = RxCacheHelper.loadCache(rxCache, key, type, true);
    Observable<CacheResult<T>> remote;
    if (isSync) {
        remote = RxCacheHelper.loadRemoteSync(rxCache, key, source, CacheTarget.MemoryAndDisk, false);
    } else {
        remote = RxCacheHelper.loadRemote(rxCache, key, source, CacheTarget.MemoryAndDisk, false);
    }
    return Observable.concat(cache, remote)
            .filter(new Predicate<CacheResult<T>>() {
                @Override
                public boolean test(@NonNull CacheResult<T> result) throws Exception {
                    return result != null && result.getData() != null;
                }
            });

@yulonghuang 确认一下:是不是在有缓存的情况下,使用cacheAndRemote策略,没有先走来自缓存的onNext(),就直接走了来自网络的onError()?
缓存不为空的话,cacheAndRemote策略是会发出2次结果的,如果后面的网络失败了,还是会走onError(),这点要注意

恩恩 实际情况确实是这样。这边只收到了一次OnError.

appApi.getChannel(mView.getTemplateId(),10)
.compose(RxCache.getDefault().<Result>transformObservable("123", new TypeToken<Result>(){}.getType(),CacheStrategy.cacheAndRemoteSync()))
.map(new CacheResult.MapFunc<Result>())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(channelResult -> {
if(channelResult!=null) {
if (channelResult !=null&&channelResult.code == 0) {
mView.showChannel(channelResult.data);
}
}
},throwable -> {

            });

直接走了异常

大哥。。有讨论群或者你的联系方式不。。咱们交个朋友。。嘿嘿

可以学习膜拜一下大佬。

RxCache.initializeDefault(new RxCache.Builder()
.appVersion(2)
.diskDir(new File(SDCardUtils.getSDCardCachePath(this) + File.separator + "data-cache"))
.diskConverter(new GsonDiskConverter())
.diskMax((20 * 1024 * 1024L))
.memoryMax(0)
.setDebug(true)
.build());

并且我这边看了一下本地确实是有缓存的

用别的策略能拿到缓存吗?

其他策略可以拿到。这也是我很奇怪的地方

到现在还是不行。cacheAndRemote 如果网络错误缓存就拿不到了 。。。

commented

@z-chu CacheStrategy.cacheAndRemote()这个缓存策略在 存在缓存网络错误 时没有走onNext,只回调了一次onError,但是在Debug调试CacheAndRemoteStrategy中的代码时,正常返回,回调一次onError一次onNext,求问一下这是什么问题