Apollo-android: [Kotlin-sample] ApolloQueryWatcher not working

Created on 23 Mar 2020  路  12Comments  路  Source: apollographql/apollo-android

Summary
I am running the Kotlin-sample app from the master branch, and integrating ApolloQueryWatcher in the current fetchRepositories call using the ApolloCallbackService

override fun fetchRepositories() {
    val repositoriesQuery = GithubRepositoriesQuery.builder()
        .repositoriesCount(50)
        .orderBy(RepositoryOrderField.UPDATED_AT)
        .orderDirection(OrderDirection.DESC)
        .build()

    val callback = object : ApolloCall.Callback<GithubRepositoriesQuery.Data>() {
      override fun onFailure(e: ApolloException) {
        exceptionSubject.onNext(e)
      }

      override fun onResponse(response: Response<GithubRepositoriesQuery.Data>) {
        repositoriesSubject.onNext(mapRepositoriesResponseToRepositories(response))
      }

      override fun onStatusEvent(event: ApolloCall.StatusEvent) {
        println("See me : " + event)
      }
    }

    apolloClient
        .query(repositoriesQuery)
        .responseFetcher(ApolloResponseFetchers.CACHE_AND_NETWORK)
        .watcher().enqueueAndWatch(callback)
  }

With the above setup, I am not seeing any callbacks on onStatusEvent call.
What am I missing here, is it expected usage or I am missing something ?

Bug

Most helpful comment

Thanks for the report. While trying to reproduce I identified the bug. I will be adding this into the sample and fix it at the same time. Let's see if we can also add a test case.

All 12 comments

Thanks for the report. While trying to reproduce I identified the bug. I will be adding this into the sample and fix it at the same time. Let's see if we can also add a test case.

@tasomaniac thank you for the fix.
Is it possible to add a sample usage of ApolloQueryWatcher in the ApolloCoroutinesService.
I stumbled upon this bug while understanding how I can use query watcher with coroutines as I need to perform some custom logic based on onComplete of onStatusEvent

I have no idea how that works. 馃槉 If you made it work, can you contribute to the sample project. If not, maybe @martinbonnin can help.

@ahetawal-p may I ask what your use case is for StatusEvent ? So far, I thought this was mainly used for debug but if there are other use cases, we should definitely document them.

@martinbonnin
I might not be thinking it the right way, but our use case is we want to use CACHE_AND_NETWORK response fetcher, and our UI needs to be refreshed for both when we get the cached response, and then again when we get the network response (with a complete finalized data identifier).
However we are trying to achieve this using Kotlin Coroutine extensions.
I was not able to find a good sample. So 2 things I am searching sample/documentation for:

  • Where should one use ApolloQueryWatcher ?
  • How do integrate CACHE_AND_NETWORK fetcher with Coroutine extension ?

Kotlin Coroutines is like RxJava's Single. I don't think they support returning multiple emissions. That's why CACHE_AND_NETWORK may not be supported with Coroutines. Could that be the case?

The Flows returned by toFlow should emit twice: once for the cache and once for the network.

Still picking up Kotlin flows, and never used RxJava before :( will it possible to see how can I one use toFlow in the ApolloCoroutinesService example.
Thank you for being so prompt on this.

Usually you would do this:

apolloCall.toFlow().collect { response ->
     // this lambda will be called twice, once for cache, once for network
}

How can i know if response is from network vs cache, as we need to show the identifier for network finalized data ?

You should have response.fromCache (https://github.com/apollographql/apollo-android/blob/c3a264ceecfc8617b5b526e297f9d6d3c1c9947c/apollo-api/src/commonMain/kotlin/com/apollographql/apollo/api/Response.kt#L34)

Thank you @martinbonnin. I think I have most of the info to move forward. Thanks again for all your pointers.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jamesweb1 picture jamesweb1  路  3Comments

john-lanticse picture john-lanticse  路  3Comments

RageshAntony picture RageshAntony  路  3Comments

gmrandom picture gmrandom  路  4Comments

TayfunCesur picture TayfunCesur  路  4Comments