I have a function capturing events from an api which works great. The only issue is i dont want the stream to be complete when it fails.
public func getDeviceEvent(deviceUID: String) -> SignalProducer<DeviceEvent, HTTPConnectorError> {
return SignalProducer<DeviceEvent, HTTPConnectorError> { observer, _ in
self.getDeviceEvent(deviceUID: deviceUID) { result in
switch result {
case .success(let deviceEvent):
observer.send(value: deviceEvent)
observer.sendCompleted()
case .failure(let error):
observer.send(error: error)
}
}
}
}
private func getDeviceEvent(deviceUID: String, completion: @escaping (Result<DeviceEvent, HTTPConnectorError>) -> Void) {
httpConnector.request(authenticated: true, target: .deviceLocationEvent(deviceID: deviceUID), parse: makeParser(), completion: completion)
}
Is there a way i can keep the stream going? i know there is retry but i don't want to add a number of times i just want it to continue.
You could create your own version of
retrywhich retries indefinitely (I've included a version here that pauses for a given interval after each failure):Then you can use
on(failed:)to display something to the user on each failure while retrying indefinitely:This snippet assumes
selfis a view controller;take(during: self.lifetime)will then stop the operation when the current view controller deallocates. You ought to do something like this to prevent this retrying forever in the worst case.