Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Combine - combining publishers without waiting for all publishers to emit first element

Tags:

ios

swift

combine

I'm combining two publishers:

let timer = Timer.publish(every: 10, on: .current, in: .common).autoconnect()
let anotherPub: AnyPublisher<Int, Never> = ...

Publishers.CombineLatest(timer, anotherPub)
    .sink(receiveValue: (timer, val) in { 
      print("Hello!")
} )

Unfortunately, sink is not called until both publishers emit at least one element.

Is there any way to make the sink being called without waiting for all publishers? So that if any publisher emits a value, sink is called with other values set to nil.

like image 649
msmialko Avatar asked Oct 29 '25 04:10

msmialko


1 Answers

You can use prepend(…) to prepend values to the beginning of a publisher.

Here's a version of your code that will prepend nil to both publishers.

let timer = Timer.publish(every: 10, on: .current, in: .common).autoconnect()
let anotherPub: AnyPublisher<Int, Never> = Just(10).delay(for: 5, scheduler: RunLoop.main).eraseToAnyPublisher()

Publishers.CombineLatest(
    timer.map(Optional.init).prepend(nil),
    anotherPub.map(Optional.init).prepend(nil)
)
.filter { $0 != nil && $1 != nil } // Filter the event when both are nil values
.sink(receiveValue: { (timer, val) in
    print("Hello! \(timer) \(val)")
})
like image 55
Charles Maria Avatar answered Oct 31 '25 18:10

Charles Maria



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!