ReactiveCocoa 4: How to send error to an observer without interrupting the signal

坚强是说给别人听的谎言 提交于 2020-01-04 05:23:50

问题


let (signal, sink) = Signal<[CLBeacon], BeaconManagerError>.pipe()

When I call this because the user disabled the Bluetooth:

sendError(self.sink, error)

the Signal is interrupted and I don't receive more next nor interrupted events after enabling the Bluetooth back again. The Signal is broken.

How can I send error types to the observer without interrupting / breaking the Signal? I can't find in the RAC 4 documentation. Thanks!


回答1:


By design, an error causes the signal to finish. The documentation says:

failures should only be used to represent “abnormal” termination. If it is important to let operators (or consumers) finish their work, a Next event describing the result might be more appropriate.

If you want to turn errors into Next events, you can use flatMapError operator as described here or use retry if you want to allow only several occurances of the error.




回答2:


If you want different semantics than Next* (Error|Completed) I recommend encoding that in the type. You can use a Signal that can't fail, but which values can be either success or failure, by using Result:

Signal<Result<[CLBeacon], BeaconManagerError>, NoError>

That signal will emit no errors, but its Next events will be Result.Success<[CLBeacon]> or Result.Failure<BeaconManagerError>, **and the signal won't terminate when receiving a Result.Failure.



来源:https://stackoverflow.com/questions/33690230/reactivecocoa-4-how-to-send-error-to-an-observer-without-interrupting-the-signa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!