Ignoring errors in Combine

In RxSwift I can ignore errors like .catchError { _ in Observable.never() }

Why does this not work in combine (eg. it still completes) .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)")
    }

finishes on the throw. The output is:

5
4
3
2
1

1 answer

  • answered 2020-07-05 12:37 Daniel T.

    I'm glad someone was able to help you in the comments, because you are asking why your code completes when it clearly doesn't.

    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 }
    

    The above clearly does not emit a "finished" event. If you change completeImmediately to true, then you will see that it does.