I have a case of "Do a network request, and then c...
# apollo-kotlin
s
I have a case of "Do a network request, and then continue watching the cache. But continue trying if the first network request has never succeeded yet" Relying on exceptions, I had something like this in the past:
Copy code
apolloClient.query(..).watch().map {
  emit(okay result)
}.retryWhen { cause, _ -> 
  val wasCacheMiss = cause is CacheMissException
  if (wasCacheMiss) {
    emit(bad result)
    delay(...)
  }
  wasCacheMiss // ^retryWhen will retry if it was a cache miss
}
Which basically translates to, Do the network request, and then observe the cache. If the network request failed, and then we get nothing from the cache, retry this entire flow, which should also retry the initial network fetch too. I am not even like 100% sure it worked exactly as I wanted it to before, but now while migrating to 4.x I wonder if I should check if the response.exception was a cache miss, throw that myself, and continue doing my retryWhen approach, or if you got a better idea for the scenario of my original scenario.
I probably should just split it up and manually do
Copy code
while(isActive) {
  val result = apollo.query().NetworkOnly.execute()
  when(result) {
    failure -> delay(...)
    success -> emit(...); break
  }
}
val result = apollo.query().fetchPolicy(FetchPolicy.CacheOnly).watch()
To just achieve what I want without having to think about this too much tbh. And this way won't have to rely on watch hitting the network initially or anything like that.
👍 1
m
Yea, that's probably the best although it seems a bit different from what you initially stated?
If the network request failed, and then we get nothing from the cache, retry this entire flow
vs
if the network request failed, retry until it passes
?
PS: there's a
watch
version that takes a
data: D?
parameter that's a bit more optimized. If you pass a data here, the watcher will only watch those fields. If not, it will watch all fields in the cache until a cache hit is done
s
If the network request failed, and then we get nothing from the cache, retry this entire flow
Ah right I see what you mean, I worded it wrong. What I wanted to say is more like "If the network request failed, and then we therefore get nothing from the cache, since the network request failed anyway so the cache was never populated...". For this case I think we only expect this cache to be populated by this particular place, not to be populated from some previous network request. Since here we do in fact want to show the most up-to-date information, we don't want to show stale information. So more like: Get the most-up-to-date info from network. Then start observing. But show error initially if the first network request fails, since we want to show info at least as fresh as of the first time you enter this screen. But yeah I will split this up, it should make more sense this way anyway. Thanks for the help, and yeah I'll try out that watch overload too 🤗
👍 1
m
Makes sense 👍