Apollo-android: How to cache request ...??

Created on 17 Oct 2019  Â·  10Comments  Â·  Source: apollographql/apollo-android

hey everyone
i want to know how can cache request if there is no internet
and retry to sending it when internet connect again ...??

waiting for info Question

All 10 comments

I think that you could find an about how to make use of http cache in this file:
https://github.com/apollographql/apollo-android/blob/master/apollo-integration/src/test/java/com/apollographql/apollo/HttpCacheTest.java

In order to retry when internet connection is available, you have to manage this at application code level to avoid any issues that could be caused by using OkHttp retry methods.

@Sparow199
thanks for your answer
but i just use apollo cache for cache response not request
sorry i i'm misunderstand
but i need a way to cache failed request
then how to fetch them again
about retry when internet connection is available
i will use work manager for this purpose
but still not find how to store and retrieve failed apollo request ..?

I think that you have to play with OkHttp interceptors to check the response status code and then you have access to request object and you could extract whatever you want from it and add it to a persisted queue in order to be retried with a long running job.

https://square.github.io/okhttp/interceptors/

@Sparow199 thanks for your comment again
i just do it like that after see some tutorial talking about that too

fun okHtpClient(){
        val client = OkHttpClient.Builder()
            .cache(Cache(this.cacheDir, 10 * 1024 * 1024)) // 10 MB
            .addInterceptor(object : Interceptor {
                @Throws(IOException::class)
                override fun intercept(chain: Interceptor.Chain): Response {
                    var request = chain.request()
                    if (Utils.isNetworkAvailable()) {

                        request =
                            request.newBuilder().header("Cache-Control", "public, max-age=" + 60)
                                .build()
                    } else {
                        request = request.newBuilder().header(
                            "Cache-Control",
                            "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 7
                        ).build()
                    }
                    return chain.proceed(request)
                }
            })
            .build()



    }

but i don't know how can retrieve request after cache it
adn how can pass it to apollo client ....?!!

You have to make the difference between caching responses and caching
requests.
In the code above, you are just caching the responses. Which is not what
you want.

You have to add else if connection is not available and extract the request
informations and store it an sqlite DB for example then you could write a
job to replay the missing requests later.

On Sat, Oct 19, 2019, 15:24 Ismail Mohamed notifications@github.com wrote:

@Sparow199 https://github.com/Sparow199 thanks for your comment again
i just do it like that after see some tutorial talking about that too
` fun okHtpClient(){
val client = OkHttpClient.Builder()
.cache(Cache(this.cacheDir, 10 * 1024 * 1024)) // 10 MB
.addInterceptor(object : Interceptor {
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
var request = chain.request()
if (Utils.isNetworkAvailable()) {

                request =
                    request.newBuilder().header("Cache-Control", "public, max-age=" + 60)
                        .build()
            } else {
                request = request.newBuilder().header(
                    "Cache-Control",
                    "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 7
                ).build()
            }
            return chain.proceed(request)
        }
    })
    .build()

}`

but i don't know how can retrieve request after cache it
adn how can pass it to apollo client ....?!!

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/apollographql/apollo-android/issues/1683?email_source=notifications&email_token=AFJ7ZUTSGBON3KNW4BII7MDQPMDBPA5CNFSM4JB46SG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBXPRHY#issuecomment-544143519,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AFJ7ZUQBF7ZLIMMGGD2ZPPDQPMDBPANCNFSM4JB46SGQ
.

@Sparow199
thanks i'll try your solution .

hey @Sparow199
i have an issue out apollo
now when request failed
i use database to store request info like function that send request and params
and use data manager to check if connection is back
i'll send this requests but
on database i store function name
my question is :
how to call function by it's string name ...?

Sorry not sure I understand the issue, could you please elaborate more?

Hey @sav007
sorry for late response
what i want to do is
when any request like (query or mutation) failed to send because of network connection
i want to cache it then when network back i want send it
what @Sparow199 suggest to me
is check if request is send or not if not
cache it in database
and check with work manager id network is back or not
then send it again
now
my issue is how to cache queries or mutations in database and resend then again ..?
and thanks for all

this is you need to implement on your end somehow, you can persist query arguments somewhere and then later reconstruct query with these arguments or you can keep query instance somewhere in memory, it's up to you and implementation.

Was this page helpful?
0 / 5 - 0 ratings