New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Retry after delay
operator
#164
Comments
it’s a good question, but without a super amazing answer as yet. for the simpler use cases, i’ve used the delay() operator in coordination with the retry() operator to get the basics in place. you can see the explicit example of this at https://heckj.github.io/swiftui-notes/#patterns-retry in the example, i’m using a random (shortish) backoff. You could just as easily make that a static/consistent delay. it’s not set up as a single operator within Combine however. the immediate question I often see is a common desire: how do i get an increasing backoff within a pipeline? and that’s where i start to fall down on anything useful. does this help highlight a solution for you? |
https://heckj.github.io/swiftui-notes/#patterns-retry inserts a delay unconditionally. How would you add a delay only if upstream publisher raises an error? It doesn't need to be a single operator, chaining delay-repeat is okay too. |
yep, it’s a downside of that implementation. if you want to handle the error and retry within the pipeline of operators, that’s the best i’ve been able to work out so far. another option is breaking out the “how you handle it” until the end of the pipeline, so invoking the retry not with the operator but with logic in a sink() subscriber where you’re specifically doing a delay and then invoking another run at whatever you wanted to try. this exposes the retry logic to your higher level code (and makes you track it manually), which also may not be what you’d like to see. |
I guess, this code will work for me:
|
That will indeed delay only on error, but a side effect of the returning a the delay from a catch and then doing the It's quite possible I incorrectly set up the test to validate this, so check it out at your convenience. The output is clearly only invoking the delay 3 times, but the underlying async call is getting double-requested on every error-catch-retry:
example/test in #165 |
Hey @anechaev - I found a slightly improved solution, and worked through the duplicate requests thing. The improved stanza moves the retry() to be inline with the The slightly better stanza is:
|
You're right, moving Maybe moving this block to the extension will look better:
|
Is there a clean way to write
Retry after delay
operator that would wait for some time and retry on error.An alternative I can imagine is to wrap throwing an error in some sort of DispatchQueue.asyncAfter and combine it with
retry
operator after.The text was updated successfully, but these errors were encountered: