I wanted to achieve something like this: Have a qu...
# apollo-kotlin
s
I wanted to achieve something like this: Have a query which does CacheAndNetwork. If I do not get a valid value back from cache, okay continue with network. If I again do not get a valid value back, delay some seconds, and then start all over again. I previously had just
CacheFirst
and I did it with smth like this:
Copy code
return flow {
 while (currentCoroutineContext().isActive) {
  val result = apolloClient.query()...
  when (result) {
   is Fail -> { delay(10.seconds) }
   is Success -> {
    emit(result...)
    break
   }
  }
 }
}
But since I am now expecting two emissions, I am having a hard time imagining how to do this. I can no longer delay on any "Fail" responses, the cache miss could be one for example which I want to just ignore in this case. Any thoughts? Have you done smth like this before? My first idea was to try and hold some local
var responseCount = 0
right inside the
flow {
block, and do
++
when I get a response, and set it back to 0 when the while loop loops, but I feel like I am making this more complicated than it should be.
b
I think using
CacheAndNetwork
makes this more complicated and I'd probably continue doing what you did previously without it
s
Yeah I'd have to manually do a cache only and a network only then perhaps. Since the reason I am migrating this was specifically that we were showing some stale stuff from the cache and I wanted to also fetch from network after we get the quick cache response 😅 I'll try the two individual responses instead tomorrow!
👍 1
Yup just did
Copy code
return flow {
  apolloClient
    .query(..)
    .fetchPolicy(FetchPolicy.CacheOnly)
    .safeExecute()
    .onRight {
      emit(successvalue)
    }
  while (currentCoroutineContext().isActive) {
    val result = apolloClient
      .query(...)
      .fetchPolicy(FetchPolicy.NetworkOnly)
      .safeExecute()
    when (result) {
      Fail -> emit(fail); delay()
      Success -> emit(result); break
    }
  }
}
And it's all good. I wanted to get fancy with flow stuff but no need tbh 😄
b
nice! 🎉