Swift Combine-等待所有发布者 [英] Swift Combine - Wait for all publishers

查看:138
本文介绍了Swift Combine-等待所有发布者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Swift组合运行具有相同结果的许多任务.目前,每个任务都是一个将发布结果的发布者.现在我面临一个问题,我必须等待所有发布者发出该元素然后继续前进.有点像调度小组.我发现zip(with:::__)运算符需要4个发布者.

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?

推荐答案

您可以使用 MergeMany 创建单个下游,以接收来自多个上游的所有发出的值,然后调用 collect()在合并的发布者上一次发出所有值.

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()

这篇关于Swift Combine-等待所有发布者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆