Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Combine - Wait for all publishers

Tags:

swift

combine

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?

like image 674
Ali Avatar asked Jan 06 '20 15:01

Ali


People also ask

Which combine operator would you use to execute some code for each pair of two publishers emitted values?

combineLatest is another operator that lets you combine different publishers. It also lets you combine publishers of different value types, which can be extremely useful.

How do I combine two publishers?

CombineLatest publisher collects the first value from all three publishers and emits them as a single tuple. CombineLatest continues sending new values even when only one publisher emits a new value. On the other hand, the Zip operator sends a new value only when all the publishers emit new values.

What is publisher in combine?

Overview. A publisher delivers elements to one or more Subscriber instances. The subscriber's Input and Failure associated types must match the Output and Failure types declared by the publisher. The publisher implements the receive(subscriber:) method to accept a subscriber.

Can we use combine in Swift?

The Combine framework provides a declarative Swift API for processing values over time. These values can represent many kinds of asynchronous events. Combine declares publishers to expose values that can change over time, and subscribers to receive those values from the publishers.

What is the combine framework in Swift combine?

Getting started with the Combine framework in Swift Combine was introduced as a new framework by Apple at WWDC 2019. The framework provides a declarative Swift API for processing values over time and can be seen as a 1st party alternative to popular frameworks like RxSwift and ReactiveSwift.

What is the use of mergemany in Swift?

As you can see, the MergeMany operator allows me to create a single pipe for cached and fresh data where the cached information usually appears first and then replaced by new data. To learn about building custom Combine operators, take a look at my “Building custom Combine operators in Swift” post.

What is a publisher in combine?

Within the world of Combine, an object that emits such asynchronous values and events is called a publisher, and although the framework does ship with quite a large number of built-in publisher implementations, sometimes we might want to build our own, custom ones in order to handle specific situations.

Can we use SwiftUI property wrappers outside SwiftUI?

However, that property wrapper can also be used outside of SwiftUI as well, and provides a way to automatically generate a publisher that emits a new value whenever a given property was changed. For example, here we’re adding that sort of functionality to an item property contained within a TodoList class:


1 Answers

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()
like image 76
Dávid Pásztor Avatar answered Oct 11 '22 02:10

Dávid Pásztor