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

编程入门 行业动态 更新时间:2024-10-24 21:21:19
本文介绍了如何从发布者数组中创建发布者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 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)] ObservablebineLatest(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]

推荐答案

只要你不要太执着于数组"部分,combineLatest 或 zip 也适用于 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 = resbineLatest(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] } }

更多推荐

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

本文发布于:2023-11-13 14:22:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584558.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发布者   组中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!