首页 > 解决方案 > 忽略组合中的错误

问题描述

在 RxSwift 中,我可以忽略错误,例如 .catchError { _ in Observable.never() }

为什么这在组合中不起作用(例如,它仍然完成) .catch { _ in Empty(completeImmediately: false) }

struct SimpleError: Error {}
let numbers = [5, 4, 3, 2, 1, 0, 9, 8, 7, 6]
let cancellable = numbers.publisher
    .tryFilter { int -> Bool in
        guard int != 0 else {throw SimpleError()}
        return true
    }
    .catch { _ in Empty(completeImmediately: false) }
    .sink {
        print("\($0)")
    }

完成投掷。输出是:

5
4
3
2
1

标签: iosswiftrx-swiftcombine

解决方案


我很高兴有人能够在评论中帮助你,因为你在问为什么你的代码显然没有完成。

struct SimpleError: Error {}
let numbers = [5, 4, 3, 2, 1, 0, 9, 8, 7, 6]
let cancellable = numbers.publisher
    .tryFilter { int -> Bool in
        guard int != 0 else {throw SimpleError()}
        return true
    }
    .catch { _ in Empty(completeImmediately: false) }
    .print("")
    .sink { _ in }

以上显然不会发出“完成”事件。如果您更改completeImmediatelytrue,那么您将看到它确实如此。


推荐阅读