Stylianos Gakis
08/15/2024, 12:33 PMapolloClient.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.Stylianos Gakis
08/15/2024, 3:55 PMwhile(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.mbonnin
08/15/2024, 8:03 PMmbonnin
08/15/2024, 8:04 PMIf the network request failed, and then we get nothing from the cache, retry this entire flowvs
if the network request failed, retry until it passes?
mbonnin
08/15/2024, 8:06 PMwatch
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 doneStylianos Gakis
08/16/2024, 3:20 PMIf the network request failed, and then we get nothing from the cache, retry this entire flowAh 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 🤗
mbonnin
08/16/2024, 3:21 PM