How do I cache content and data from a json API using Rx2AndroidNetworking?

525 Views Asked by At

I am retrieving my response from my API with rxjava with funcs as follows:

@Override
public Single<MyInfoResponse> getMyInfoApiCall() {
    return Rx2AndroidNetworking.get(ApiEndPoint.ENDPOINT_MY_INFO)
            .addHeaders(mApiHeader.getProtectedApiHeader())
            .build()
            .getObjectSingle(MyInfoResponse.class);
}

Now ,I am retrieving this data and using it in my UI code( with usual compositedisposables) as follows:

 @Override
    public void onViewPrepared() {
        getCompositeDisposable().add(getDataManager()
                .getMyInfoApiCall()
                .subscribeOn(getSchedulerProvider().io())
                .observeOn(getSchedulerProvider().ui())
                .subscribe(myInfoResponse -> {
                    getDataManager().updateMyManagerInfo(myInfoResponse);
                    if (myInfoResponse != null && myInfoResponse.getData() != null) {
                        getMvpView().updateMyRepo(myInfoResponse.getData());
                    }

                }, throwable -> {
                    if (!isViewAttached()) {
                        return;
                    }

                    getMvpView().hideLoading();

                    // handle the error here
                    if (throwable instanceof ANError) {
                        ANError anError = (ANError) throwable;
                        handleApiError(anError);
                    }
                }));
    }

Now everytime I have internet connectivity and it tries to retrieve data it works fine, but as soon as I lose internet connectivity, I am trying to cache this data so it still displays on the UI until the network connectivity is back and ready to update data real time. How do I go about this in the easiest and the most ideal way possible?

1

There are 1 best solutions below

2
Abraham Mathew On

I suggest you to use offline support using android Room Persistence. Have a look at the google sample from the below link
https://developer.android.com/topic/libraries/architecture/room

In this case
Prefetch a request (so that it can return from cache when required at instant)

AndroidNetworking.get("https://fierce-cove-29863.herokuapp.com/getAllUsers/{pageNumber}")
                .addPathParameter("pageNumber", "0")
                .addQueryParameter("limit", "30")
                .setTag(this)
                .setPriority(Priority.LOW)
            .build()
            .prefetch();
  • First of all the server must send cache-control in header so that is starts working.

  • Response will be cached on the basis of cache-control max-age,max-stale.

  • If internet is connected and the age is NOT expired it will return from cache.

  • If internet is connected and the age is expired and if server returns 304(NOT MODIFIED) it will return from cache.

  • If internet is NOT connected if you are using getResponseOnlyIfCached() - it will return from cache even it date is expired.

  • If internet is NOT connected , if you are NOT using getResponseOnlyIfCached() - it will NOT return anything.

  • If you are using getResponseOnlyFromNetwork() , it will only return response after validation from server.

  • If cache-control is set, it will work according to the max-age,max-stale returned from server.

  • If internet is NOT connected only way to get cache Response is by using getResponseOnlyIfCached().

    I just went through the docs:
    Refer here