Created
August 27, 2020 13:51
-
-
Save nakiwo/31ee9c3047f468b39dd3dfa59ac26e27 to your computer and use it in GitHub Desktop.
combine_retry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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