Apollo-android: Returning cache expired data if there is no network

Created on 5 Aug 2020  路  7Comments  路  Source: apollographql/apollo-android

Hi,

I want a query to be cached during 12h, after 12h I want it to make a request to the server. The issue is that I also need to be able to return the data when there is no connection and in some cases, the cache will be expired and will return an error.

This is my code:

kotlin apolloClient.query(query) .httpCachePolicy(HttpCachePolicy.CACHE_FIRST.expireAfter(12, TimeUnit.HOURS))

I need to configure the cache in such a way that data is only requested to the server every 12h but cached data is returned if the user has no internet (even when 12h passed since the last successful request).

Is there a way to accomplish this? Can I configure the cache to don't remove the expired data (only setting a flag "expired" to it) so it can be reused?

Thanks!

Question

All 7 comments

Hi 馃憢 I'm afraid this is not possible using the Apollo APIs but maybe this can be done using the OkHttp cache. ApolloClient takes a OkHttpClient parameter so you could maybe configure the cache there?

Thanks a lot for the quick answer @martinbonnin. I found a very old issue where there is a discussion that could be related to this topic. The issue was closed and the API has changed a lot in the last years, do you know if there are plans to implement something like NETWORK_BEFORE_STALE policy?

Signals the apollo client to first fetch the data from the network request. If the network request fails, then the data is fetched from the http cache even if it has become stale.

There are a lot of improvements we'd like to do to the cache that we're trying to keep track of there: https://github.com/apollographql/apollo-android/issues/2331.

Most of them are around the normalized cache at this point so it'll most likely come first.

Do you think this change could be done to the HTTP cache? Is there a list of improvements for the HTTP cache?

The above ticket is a mixed bag that lists everything cache-related (HTTP and normalized, memory and SQL, etc...).

Changes to the HTTP cache can definitely be done but I have no idea when that could land.

Then, can you add this to the list? I think is better to have it there and close this issue, what do you think?

It should be there already :-). It's called Network before stale but I can rename if needed.

Sorry I just saw it. We can close this issue then. Thanks!!

Was this page helpful?
0 / 5 - 0 ratings