Combine: inconsistent share() behavior

During my learning of retry and share operators, I composed the following codes:

enum MyError: Error {
    case testError
}    

[1,2].publisher
.tryMap {
    if $0 > 1 { throw MyError.testError }
    return $0
}
.retry(2)
.sink(receiveCompletion: { print($0) },
      receiveValue: { print($0) })

Prints fulfills expectation,

1
1
1
failure(__lldb_expr_485.MyError.testError)

However, when I add a share operator, the result is confusing to me.

[1,2].publisher
.share()
.tryMap {
    if $0 > 1 { throw MyError.testError }
    return $0
}
.retry(2)
.sink(receiveCompletion: { print($0) },
      receiveValue: { print($0) })

// 1
// 1
// finished

It seems like the retry is executed once, and then mysteriously succeeds.

Terms of Service

Privacy Policy

Cookie Policy