VictorAlbertos / RxCache

Reactive caching library for Android and Java
Apache License 2.0
2.37k stars 192 forks source link

No data saved in PERSISTENCE #130

Open ildar2 opened 6 years ago

ildar2 commented 6 years ago

There are three Source types: MEMORY, PERSISTENCE and CLOUD; I get results from cloud and memory, but after I close app I lose all data: no data returned from PERSISTENCE. How can I fix/debug this?

UmarBhutta commented 6 years ago

Same is happening with me.

leejiang1 commented 5 years ago

I had the same when data encrypted

SmallerRiver commented 5 years ago

There are three Source types: MEMORY, PERSISTENCE and CLOUD; I get results from cloud and memory, but after I close app I lose all data: no data returned from PERSISTENCE. How can I fix/debug this? Are you solve it? Can you answer me quickly ?

ildar2 commented 5 years ago

Yeah, I used something else

ghost commented 5 years ago

What method can you use to achieve the same effect, thanks

ildar2 commented 5 years ago

I've created interface MbCache to store my object (called Dashboard). Implementation can be any persistence lib. then I just manually saved and retrieved it

    @Inject
    protected MbCache mCache;

    @Inject
    public DataRepository() {
    }

    private boolean dashboardCacheValid;
    private boolean firstTimeDashboardRequested = true;

    public void invalidateDashboardCache() {
        //todo for LocalCache
        Timber.v("dashboard cache invalidated");
        firstTimeDashboardRequested = false;
        dashboardCacheValid = false;
    }

    public Observable<Dashboard> getDashboard(String phoneNum, boolean getNewData) {
        Dashboard fromCache = mCache.getDashboard();
        if (firstTimeDashboardRequested) {
            dashboardCacheValid = fromCache.cacheValid();
            firstTimeDashboardRequested = false;
        }
        if (!fromCache.cacheValid()) invalidateDashboardCache();
        if (dashboardCacheValid && !getNewData) {
            return Observable.just(fromCache);
        } else {
            Observable<Dashboard> observableToCache = Observable.concat(Observable.just(fromCache),
                    mNetworkHelper.getDashboardRaw(phoneNum)
                            .doOnNext(dashboard -> {
                                dashboardCacheValid = true;
                                mCache.putDashboard(dashboard);
                            }));
            return cacheObservable(CACHE_PREFIX_GET_DASHBOARD + phoneNum, observableToCache);
        }
    }
ghost commented 5 years ago

I've created interface MbCache to store my object (called Dashboard). Implementation can be any persistence lib. then I just manually saved and retrieved it

    @Inject
    protected MbCache mCache;

    @Inject
    public DataRepository() {
    }

    private boolean dashboardCacheValid;
    private boolean firstTimeDashboardRequested = true;

    public void invalidateDashboardCache() {
        //todo for LocalCache
        Timber.v("dashboard cache invalidated");
        firstTimeDashboardRequested = false;
        dashboardCacheValid = false;
    }

    public Observable<Dashboard> getDashboard(String phoneNum, boolean getNewData) {
        Dashboard fromCache = mCache.getDashboard();
        if (firstTimeDashboardRequested) {
            dashboardCacheValid = fromCache.cacheValid();
            firstTimeDashboardRequested = false;
        }
        if (!fromCache.cacheValid()) invalidateDashboardCache();
        if (dashboardCacheValid && !getNewData) {
            return Observable.just(fromCache);
        } else {
            Observable<Dashboard> observableToCache = Observable.concat(Observable.just(fromCache),
                    mNetworkHelper.getDashboardRaw(phoneNum)
                            .doOnNext(dashboard -> {
                                dashboardCacheValid = true;
                                mCache.putDashboard(dashboard);
                            }));
            return cacheObservable(CACHE_PREFIX_GET_DASHBOARD + phoneNum, observableToCache);
        }
    }

When I manually clear the application's cache and then retrieve the data from the rxcache again, I will not get a data error

W/System.err: io.rx_cache2.RxCacheException: The Loader provided did not return any data and there is not data to load from the Cache getJsonFromInternet 2019-03-22 11:58:06.117 29792-29792/com.open.filter W/System.err: at io.rx_cache2.internal.ProcessorProvidersBehaviour$5.apply(ProcessorProvidersBehaviour.java:148) 2019-03-22 11:58:06.117 29792-29792/com.open.filter W/System.err: at io.reactivex.internal.operators.observable.ObservableOnErrorReturn$OnErrorReturnObserver.onError(ObservableOnErrorReturn.java:72) 2019-03-22 11:58:06.117 29792-29792/com.open.filter W/System.err: at io.reactivex.internal.observers.BasicFuseableObserver.onError(BasicFuseableObserver.java:100) 2019-03-22 11:58:06.117 29792-29792/com.open.filter W/System.err: at io.reactivex.internal.observers.BasicFuseableObserver.fail(BasicFuseableObserver.java:110) 2019-03-22 11:58:06.117 29792-29792/com.open.filter W/System.err: at io.reactivex.internal.operators.observable.ObservableMap$MapObserver.onNext(ObservableMap.java:59) 2019-03-22 11:58:06.118 29792-29792/com.open.filter W/System.err: at com.jakewharton.retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:51) 2019-03-22 11:58:06.118 29792-29792/com.open.filter W/System.err: at com.jakewharton.retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:37) 2019-03-22 11:58:06.118 29792-29792/com.open.filter W/System.err: at com.jakewharton.retrofit2.adapter.rxjava2.CallObservable.subscribeActual(CallObservable.java:43)