How to combine Combine publishers?

Given the following

var separateTimers = [1.0,2.0,3.0].map{ n in
  Timer.publish(every: n, on: RunLoop.main, in: .default)
    .map{ x in n }

var mergedTimers = SomePublisher(separateTimers)

mergedTimers.sink{ value in print(value) }

What should be on the SomePublisher... line to merge the separateTimers into a single stream?

I've looked at the following:

merge - only combines two publisher not a list
append - doesn't have the contentsOf option that Array.append has

but I'm looking to interleave the values from multiple publishers. Which Publisher/operator am I looking for?

I think you want Publishers.MergeMany.

let mergedTimers = Publishers.MergeMany(separateTimers)

That does the job nicely, thanks!

Terms of Service

Privacy Policy

Cookie Policy