Skip to content

Instantly share code, notes, and snippets.

@nakiwo
Created August 27, 2020 13:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nakiwo/31ee9c3047f468b39dd3dfa59ac26e27 to your computer and use it in GitHub Desktop.
Save nakiwo/31ee9c3047f468b39dd3dfa59ac26e27 to your computer and use it in GitHub Desktop.
combine_retry
import Combine
import Foundation
enum CustomError: Error {
case retryError
case otherError
}
var count = 0
let publisher = Deferred {
Future<String, CustomError> { callback in
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
switch count {
case 2:
callback(.failure(.otherError))
case let n where n < 3:
callback(.failure(.retryError))
default:
callback(.success("**(\(count))**"))
}
count += 1
}
}
}
let cancellable = publisher
.print("DEBUG")
.retry(5)
.sink(receiveCompletion: {
print("receiveCompletion \($0)")
}, receiveValue: {
print("receiveValue \($0)")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment