There is one important cache strategy missing: first cache (if not expired) and then network.
I want Apollo to first emit data from cache (if present and not expired) and always make a network call. With current policies, I have to use 2 calls with policies: CACHE_ONLY and NETWORK_ONLY. It's very useful when I want to display data ASAP but always update my data.
It could be named as CACHE_AND_NETWORK.
The CACHE_FIRST policy is not the one I need: it doesn't make a network call if there is a valid cache.
Are you referencing to com.apollographql.apollo.api.cache.http.HttpCachePolicy#CACHE_FIRST?
Yes, I referred to com.apollographql.apollo.api.cache.http.HttpCachePolicy#CACHE_FIRST, but it's not the behavior I'd like to have.
As written in the docs, CACHE_FIRST signals the Apollo client to first fetch the GraphQL query response from the HTTP cache. If it's not present in the cache response is fetched from the network. Which means, that if there is a valid cache it will never make a network call.
The exactly same thing about the com.apollographql.apollo.fetcher.ResponseFetcher and CacheFirstFetcher.
@k-misztal
You can try combining CACHE_ONLY and NETWORK_ONLY requests using RX's combineLatest for example.
That's what I do at the moment in my project. To be precise I use Observable.concatArrayEagerDelayError which makes it work perfect - it emits cache and makes a network call immediately. Plus it delays error so if the cache is broken it'll still make a network call.
But because Rx makes it simple it doesn't mean that it shouldn't be provided out of the box by Apollo. Some people don't know Rx well :)
The behavior I described is a default behavior in Firebase Firestore and in my opinion, it would be nice to have it in Apollo too.
The iOS apollo lib has this feature policy too, so I would like to see the same on Android as well
Sorry maybe I'm missing something but why can't you use com.apollographql.apollo.api.cache.http.HttpCachePolicy#CACHE_ONLY#expireAfter
How to use it to get the behaviur I described? Sorry, it's not clear for me.
I don't want to expire cache efter some time. I want cache always to be emitted before network. Exactly like @disparate said: combination of CACHE_ONLY and NETWORK_ONLY
I think what you wanna try is adding a responseFetcher with CACHE_AND_NETWORK policy .build()).responseFetcher(ApolloResponseFetchers.CACHE_AND_NETWORK).enqueue(callback);
Any updates on this? I want to achieve the same strategy, not sure if I get exactly how should I use the code provided by @paatz04 .
Any updates on this?
I have used it and it work, yeah!
@hidabe hmm?
I have used it and it work, yeah!
Is the new policy available somewhere?
Isn't this already possible if you use custom responseFetcher? Please try adding the following to your ApolloClient
responseFetcher(ApolloResponseFetchers.CACHE_AND_NETWORK)
Looking at the following tests cases and the JavaDocs, this should be what you need. Please try and open a new issue if it doesn't work as expected.
https://github.com/apollographql/apollo-android/blob/master/apollo-integration/src/test/java/com/apollographql/apollo/internal/fetcher/CacheAndNetworkFetcherTest.java
https://github.com/apollographql/apollo-android/blob/master/apollo-runtime/src/main/java/com/apollographql/apollo/fetcher/ApolloResponseFetchers.java
I'm going to create an issue to document this better.
Most helpful comment
Any updates on this? I want to achieve the same strategy, not sure if I get exactly how should I use the code provided by @paatz04 .