Skip to content

Instantly share code, notes, and snippets.

@Joony
Created January 3, 2023 08:38
Show Gist options
  • Save Joony/4d3fd2dcd1087d9a644787a30494f87d to your computer and use it in GitHub Desktop.
Save Joony/4d3fd2dcd1087d9a644787a30494f87d to your computer and use it in GitHub Desktop.
A Combine publisher where you can specify a time interval between retries.
import Combine
extension Publisher {
func retryWithDelay<T, E>(retries: Int, interval: DispatchQueue.SchedulerTimeType.Stride) -> Publishers.Catch<Self, AnyPublisher<T, E>> where T == Self.Output, E == Self.Failure {
self.catch { error -> AnyPublisher<T, E> in
Publishers.Delay(
upstream: self,
interval: interval,
tolerance: 1,
scheduler: DispatchQueue.global()
)
.retry(retries)
.eraseToAnyPublisher()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment