I'm having array of Bool Observables in Rxswift.
let rxBoolObservableArray: [Observable<Bool>] = [Observable<Bool>]()
Now, How to get If any of the element is false?
func containsFalse(array: [Observable<Bool>]) -> Observable<Bool> {
return Observable.combineLatest(array) { $0.contains(false) }
}
The combineLatest
function will subscribe to all the observables in the array.
The above will also update the array every time one of the observables updates its value so the output will always be correct. The accepted answer doesn't do that (it only works for the Observable.just
function and is incorrect.)
Here is allSatisfy
extension based on @DanielT answer. It might be suitable for your problem:
extension Array where Iterator.Element: ObservableType {
func allSatisfy(_ predicate: @escaping (Iterator.Element.E) throws -> Bool) -> Observable<Bool> {
return Observable.combineLatest(self) { try $0.allSatisfy(predicate) }
}
}
example usage:
rxBoolObservableArray
.allSatisfy { $0 } // { $0 == true }
.subscribe(onNext: { areTestsPassing in
print(areTestsPassing)
})
.disposed(by: disposeBag)