I have found ways of combining publishers using MergeMany or CombineLatest, but I don't seem to find a solution in my particular case.
Example:
class Test {
@Published var firstNameValid: Bool = false
@Published var lastNameValid: Bool = false
@Published var emailValid: Bool = false
@Published var allValid: Bool = false
}
I want allValid to become false when any of the previous publishers are set to false, and true if all of them are true.
I also don't want to hardcode the list of publishers I am observing since I want a flexible solution, so I want to be able to pass an array of Bool publishers to whatever code I use to do this.
I tried this
let fieldPublishers = [$firstNameValid, $lastNameValid, $emailValid]
Publishers
.MergeMany(fieldPublishers)
.sink { [weak self] values in
self?.allValid = values.allSatisfy { $0 }
}
.store(in: &subscribers)
But this of course doesn't work because I get an array of publishers and not an array of values. I tried some other ways (forgot which ones) but they only seemed to call sink if I assigned a value during execution to all 3 publishers.
In the case of only using 2 publishers I managed to get it working using CombineLatest.
So the question is: Can I have sink triggered when only one of the publishers in an array changes value after instantiation of Test, and then iterate over the values of all the publishers I am observing?