Assume the two following publishers:
var firstFeed = PassthroughSubject<Int?, Never>()
var secondFeed = PassthroughSubject<Int?, Never>()
How do I create a subscription where the sink is called when the two feed provide two none-nil values. This is what I have so far, but it seems ugly.
let sub = firstFeed
.compactMap{$0}
.combineLatest(secondFeed.compactMap({$0}))
.sink { firstValue, secondValue in
print("we sunk with \(firstValue), \(secondValue)")
}
With the following stream of data:
firstFeed.send(1)
secondFeed.send(nil)
secondFeed.send(2)
firstFeed.send(nil)
firstFeed.send(3)
firstFeed.send(nil)
secondFeed.send(nil)
firstFeed.send(nil)
secondFeed.send(nil)
secondFeed.send(4)
firstFeed.send(nil)
secondFeed.send(nil)
firstFeed.send(5)
secondFeed.send(6)
secondFeed.send(nil)
firstFeed.send(nil)
firstFeed.send(7)
firstFeed.send(nil)
secondFeed.send(8)
I get this output:
we sunk with 1, 2
we sunk with 3, 2
we sunk with 3, 4
we sunk with 5, 4
we sunk with 5, 6
we sunk with 7, 6
we sunk with 7, 8
ideally I would want something like this:
let sub = firstFeed
.combineLatest(secondFeed)
.compactMap{?}
.sink { firstValue, secondValue in
print("we sunk with \(firstValue), \(secondValue)")
}
I'm not sure what goes into the compact map when two publishers are combined...