19

I am trying to use Swift combine to run many tasks with the same result. at the moment each task is a publisher that will emit a result. now I am facing a problem that I have to wait for all publishers to emit the element then moving on. kind of like dispatch group. I found zip(with:::_) operator which takes 4 publishers.

https://developer.apple.com/documentation/combine/passthroughsubject/3333571-zip

but what if you have an array of publishers (in case that they emit the same kind of element) ? is there any way to do that?

5
  • So what do you want your downstream publisher to emit and when? Should it emit all values emitted by its upstreams at once in an array once all upstreams finished successfully? Or do you want them to be emitted one-by-one as they are emitted from the upstreams and then make the downstream complete once all upstreams completed? Jan 6, 2020 at 15:59
  • @DávidPásztor I want my downstream emitted all values once in an array or any other collection, after all upstreams emitted a value
    – Ali
    Jan 6, 2020 at 16:20
  • What does “if you have an array of publishers” mean?show some code.
    – matt
    Jan 6, 2020 at 17:10
  • you can use multicast operator here is reference heckj.github.io/swiftui-notes/#coreconcepts Jan 16, 2020 at 13:04
  • What if some publishers emit more than one element while waiting for all to emit? What should be the behaviour in this case?
    – Cristik
    Jun 29, 2021 at 10:52

2 Answers 2

32

You can use MergeMany to create a single downstream receiving all emitted values from several upstreams and then call collect() on the merged publisher to emit all values at once.

let pubs = [Just(1),Just(2),Just(3)]
let downstream = Publishers.MergeMany(pubs).collect()
1

Well you can use CombineLatest. You can input upto 3 publisher properties and have a single downstream which will be executed once all publishers are received.

As per the example given in WWDC you can use it as follows.

enter image description here

class ViewController: UIViewController {
    @IBOutlet private var termsSwitch: UISwitch!
    @IBOutlet private var privacySwitch: UISwitch!
    @IBOutlet private var nameField: UITextField!
    @IBOutlet private var submitButton: UIButton!

    @Published private var acceptedTerms: Bool = false
    @Published private var acceptedPrivacy: Bool = false
    @Published private var name: String = ""

    // Keep a reference to the subscription so it's
    // only cancelled when we are deallocated.
    private var termsStream: AnyCancellable?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        termsStream = validToSubmit
          .receive(on: RunLoop.main)
          .assign(to: \.isEnabled, on: submitButton)

    }

    @IBAction private func acceptTerms(_ sender: UISwitch) {
        acceptedTerms = sender.isOn
    }

    @IBAction private func acceptPrivacy(_ sender: UISwitch) {
        acceptedPrivacy = sender.isOn
    }

    @IBAction private func nameChanged(_ sender: UITextField) {
        name = sender.text ?? ""
    }

    private var validToSubmit: AnyPublisher<Bool, Never> {
        return Publishers.CombineLatest3($acceptedTerms, $acceptedPrivacy, $name)
            .map { terms, pricacy, name in
                terms && pricacy && !name.isEmpty
            }.eraseToAnyPublisher()
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.