Question about potentially continuously re-running...
# apollo-kotlin
s
Question about potentially continuously re-running an apollo request on a loop while it does not return what I want it to, so I want to keep retrying 🧵
I currently have something like this:
Copy code
return flow {
 var didReturnSuccessfulResponse = false
 while (!didReturnSuccessfulResponse) {
  apolloClient
   .query(...)
   .fetchPolicy(FetchPolicy.NetworkOnly)
   ...
   .collect {
    if (it.isRight()) {
     didReturnSuccessfulResponse = true
    }
    emit(it)
   }
  delay(5.seconds)
 }
}
Does this look reasonable? I initially thought of using the
.retry
flow operator, but that seems to work only when there are exceptions involved, so I do not think this is what I want here
retryWhen
or
retry
feel like the perfect API here to wrap this sort of looping logic, but for me to be able to use em I'd need to instead
throw
inside of my mapping function or something like that, which feels like the wrong thing to do here. Are there any smarter ideas here or is my silly
while loop
the best I can do?
m
I'm team
while-loop
. I think
retry {}
is misleading actually. Exceptions shouldn't be used for flow control as they are expensive and type-unsafe
s
I think a
retryWhen
sort of flow operator which restarts the flow, but on a predicate which is not an exception would be nice. The predicate could be on the emitted values instead. But in any case, the simple loop looks to be working fine for me, so I will keep it nice and simple here ^^
b
lgtm
m
takeWhile {}
+
onCompletion {}
might be able to help but TBH, I feel it's one of those cases like when everyone was
flatMapping{}, apply{}, run {}, etc...
in the early days of Kotlin when in the end, it's just a for loop
😅 1
<insert monk + gaussian meme>
😂 1