I would like the first() operator to send a value as soon as it's received a value that satisfies the condition (and to finish publishing as well). But I don't know how to test this is really happening. The only test I've devised is below, but I suspect it is wrong. The map operator will keep demanding values of course, so I expect it to print all of them. Yet the sink subscriber doesn't invoke receiveValue or receiveCompletion until after map is done, which suggests first is awaiting upstream completion. Does first really does need the upstream to complete before it sends anything onward? What's really the story here for first?
My specific situation is I'm monitoring for when the network becomes available the first time. Of course the monitoring source will never (should never) finish but I do want completion for a pipeline subscribing to that monitor with first.
Code Block import Foundation import Combine let numbers = (-10...10) let cancellable = numbers.publisher .map({ n in Thread.sleep(forTimeInterval: 1) print("sending \(n)") return n }) .first { $0 > 0 } .sink { completion in switch completion { case .failure(_): print("failed") case .finished: print("finished") } } receiveValue: { print("\($0)") }
My specific situation is I'm monitoring for when the network becomes available the first time. Of course the monitoring source will never (should never) finish but I do want completion for a pipeline subscribing to that monitor with first.