I want to make a network request more than one time when some error occurs using retry() from Swift/Combine. The block inside the publisher is called once only which means one only one request is made for a real app when error happens. My code is:
import UIKit
import Combine
import PlaygroundSupport
enum TestFailureCondition: Error {
case invalidServerResponse
}
var backgroundQueue: DispatchQueue = DispatchQueue(label: "backgroundQueue")
var failPublisher: AnyPublisher<(Data, URLResponse), Error> {
Future<(Data, URLResponse), Error> { promise in
print("Attempt to call")
backgroundQueue.asyncAfter(deadline: .now() + Double.random(in: 1..<3)) {
promise(.failure(TestFailureCondition.invalidServerResponse))
}
}
.eraseToAnyPublisher()
}
let cancellable = failPublisher
.print("(1)>")
.retry(3)
.print("(2)>")
.sink(receiveCompletion: { fini in
print(" ** .sink() received the completion:", String(describing: fini))
PlaygroundPage.current.finishExecution()
}, receiveValue: { stringValue in
print(" ** .sink() received \(stringValue)")
})
PlaygroundPage.current.needsIndefiniteExecution = true
I expect that backgroundQueue.asyncAfter(deadline)
is called three time before some error happens. Does anyone know why?