如何从发布者数组中创建发布者?

问题描述:

我正在使用 Swift Combine 并且想要制作一个从 Publisher 数组创建的 Publisher.每次数组中的一个发布者发出一个新元素时,它都应该发出一个元素数组.

I am using Swift Combine and would like to make a Publisher that was created from an array of Publisher. It should emit an array of elements every time one of the Publishers in the array emits a new element.

RxSwift 中将如下所示:

 let obs1 = PublishSubject<Int>()
 let obs2 = PublishSubject<Int>()
 let arrayObs = [obs1, obs2)]

 Observable.combineLatest(arrayObs)
    .subscribe(onNext: { arrayOfLatest in
        print(arrayOfLatest) //prints an array of integers
    }).disposed(by: disposeBag)

obs1.onNext(5) 
obs2.onNext(10) // prints [5,10]
obs1.onNext(12) // prints [12,10]

只要你不要太执着于数组"部分,combineLatestzip 也适用于 Combine 框架.它们之间的区别在于您是否希望每次发出的总体值包含 oldest (zip) 或 newest (combineLatest) 每个出版商的贡献.

As long as you don't insist too strongly on the "array" part, combineLatest or zip is correct for the Combine framework too. The difference between them is whether you want the overall value emitted each time to contain the oldest (zip) or the newest (combineLatest) contribution from each publisher.

你的例子实际上并没有提供足够的信息来告诉你想要哪些.要知道你想要哪个,你需要说当你说时应该发生什么,例如:

Your example doesn't actually give enough information to tell which of those you want. To know which you want, you'd need to say what should happen when you say e.g.:

obs1.onNext(5) 
obs1.onNext(6) 
obs2.onNext(10)

尽管如此,Combine 是 Swift,所以它使用 元组, 而不是数组来表达值的整体.但是,如果在您的示例中,所有发布者都具有相同的输出和故障类型,则可以通过映射轻松替换数组.请参阅如何压缩 4 个以上的发布者,了解如何将 zip 到一个数组处理器.完全相同的技术适用于 combineLatest.

Be that as it may, Combine is Swift, so it uses a tuple, not an array, to express the totality of values. But if, as in your example, all the publishers have the same output and failure types, then an array can easily be substituted by mapping. See How to zip more than 4 publishers for an example turning zip into an array processor. Exactly the same technique works for combineLatest.

所以,这是一个实际的工作示例;为了使示例比您的示例更通用,我使用了三个发布商而不是两个:

So, here's an actual working example; to make the example more general than yours, I used three publishers instead of two:

class ViewController: UIViewController {
    let obs1 = PassthroughSubject<Int,Never>()
    let obs2 = PassthroughSubject<Int,Never>()
    let obs3 = PassthroughSubject<Int,Never>()
    var storage = Set<AnyCancellable>()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let arr = [obs1, obs2, obs3]
        let pub = arr.dropFirst().reduce(into: AnyPublisher(arr[0].map{[$0]})) {
            res, ob in
            res = res.combineLatest(ob) {
                i1, i2 -> [Int] in
                return i1 + [i2]
            }.eraseToAnyPublisher()
        }
        pub.sink { print($0) }.store(in: &storage)
        obs1.send(5)
        obs2.send(10)
        obs3.send(1) // [5, 10, 1]
        obs1.send(12) // [12, 10, 1]
        obs3.send(20) // [12, 10, 20]
    }
}