Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI and CombineLatest with more than 4 values

I'm starting to experiment with SwiftUI and I have a situation where I want the latest combination of 5 sliders. I had everything working with 4 sliders, using CombineLatest4, then realized I need another slider, but there's no CombineLatest5.

Any help appreciated.

To clarify the working 4-slider version:

Publishers
    .CombineLatest4($slider1, $slider2, $slider3, $slider4)
    .debounce(for: 0.3, scheduler: DispatchQueue.main)
    .subscribe(subscriber)
like image 438
jbm Avatar asked May 12 '20 01:05

jbm


2 Answers

CombineLatest2(CombineLatest3, CombineLatest2) should do the trick, shouldn't it?

like image 94
Daniel T. Avatar answered Oct 31 '22 11:10

Daniel T.


Note - solution extracted from the question


Okay, getting the syntax figured out, @Daniel-t was right. I just needed to create the sub-publishers:

let paramsOne = Publishers
    .CombineLatest3($slider1, $slider2, $slider3)
        
let paramsTwo = Publishers
    .CombineLatest($slider4, $slider5)
        
paramsOne.combineLatest(paramsTwo)
    .debounce(for: 0.3, scheduler: DispatchQueue.main)
    .subscribe(subscriber)

Note that I also had to change what my subscriber expected as input from (Double, Double, Double, Double, Double) to ((Double, Double, Double), (Double, Double)), and that the compiler gave me a misleading and confusing error (something about Schedulers) until I figured out that the input type was wrong.

like image 2
Cristik Avatar answered Oct 31 '22 11:10

Cristik