1
votes

I'm using RxCache on my Android project and I have a problem when the user has no internet connection.

Is there a way to prevent an observable from getting evicted if no data is returned from the server?

For example, each time the user refreshes a news feed (pull to refresh) getRepository().getFeedPosts(tag, new EvictProvider(true)); gets called.

If the user suddently loses his connection to the internet and refreshes the feed again, no data gets returned from the server, the observable gets evicted and it returns the cached version (since I'm setting useExpiredDataIfLoaderNotAvailable(true) on the RxCache builder).

Again with no internet connection, if the user refreshes a second time, no cached data is available.

Is there a way to prevent that from happening?

Thanks

2
Is there an exception if you loose the connection and try to get data from the server or any other kind of indication? You could probaly filter invalid data. If it is invalid, you would not evade it. I would need more code to see whats going on. - Hans Wurst
There are no exceptions. From what I can tell, it's a RxCache limitation. - cordeiro

2 Answers

0
votes

Answering my own question, I guess it's a RxCache limitation.

This is what Victor Albertos from RxCache told me:

I don't think that's possible. Precisely, this is the way you evict the data of some provider, by creating an observable that just throws. I admit this is not optimal, but if you want to use a more mature API which handles these scenarios properly, I recommend to you to use ReactiveCache

0
votes

I ran into the exact same problem and ended up simply adding an internet availability check to decide whether or not the cache should be invalidated.

public Boolean isOnline() {
    MyApplication application = MyApplication.sharedApplication();
    ConnectivityManager cm =
            (ConnectivityManager) application.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

In conjunction with my cached api call like so:

public static  Single<List<People>> getPeopleAttending() {
    PartyManager manager = sharedManager();
    manager.cacheProvider.getPeopleAttending(manager.partyService.getAttending("Party/PeopleAttending"), new EvictProvider(isOnline()));
}

This ensures that if my app is ever offline, it will get the cached results. If it is online, it will always get the current list of people attending the party from the server.