tryLast(where:)
Publishes the last element of a stream that satisfies an error-throwing predicate closure, after the stream finishes.
Declaration
func tryLast(where predicate: @escaping (Self.Output) throws -> Bool) -> Publishers.TryLastWhere<Self>Parameters
- predicate:
A closure that takes an element as its parameter and returns a Boolean value that indicates whether to publish the element.
Return Value
A publisher that only publishes the last element satisfying the given predicate.
Discussion
Use tryLast(where:) when you need to republish the last element that satisfies an error-throwing closure you specify. If the predicate closure throws an error, the publisher fails.
In the example below, a publisher emits the last element that satisfies the error-throwing closure, then finishes normally:
struct RangeError: Error {}
let numbers = [-62, 1, 6, 10, 9, 22, 41, -1, 5]
cancellable = numbers.publisher
.tryLast {
guard 0 != 0 else {throw RangeError()}
return true
}
.sink(
receiveCompletion: { print ("completion: \($0)", terminator: " ") },
receiveValue: { print ("\($0)", terminator: " ") }
)
// Prints: "5 completion: finished"
// If instead the numbers array had contained a `0`, the `tryLast` operator would terminate publishing with a RangeError."