Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine an array of signals into one #3158

Closed
lxian opened this issue Aug 29, 2016 · 6 comments
Closed

Combine an array of signals into one #3158

lxian opened this issue Aug 29, 2016 · 6 comments
Labels

Comments

@lxian
Copy link

lxian commented Aug 29, 2016

Hi,

I have an array of network requests signalProducers and each outputs a string and I would like to combine them into one signalProducer which output an array of string (convert [SignalProducer<String, NSError>] into SignalProducer<[String], NSError>)

I searched and found combineLatest, but it results in a tuple. Is there any way to make it an array ? Thanks in advance.

@andersio
Copy link
Member

SignalProducerProtocol.merge and collect should do the job.

@lxian
Copy link
Author

lxian commented Aug 29, 2016

@andersio But I want to preserve the ordering of the original array of signalProducers in the combined signalProducer. collect looks like collecting the values in the order that events get fired.

@lxian
Copy link
Author

lxian commented Aug 29, 2016

@andersio I write it currently like

arrayOfSignalProducers.reduce(SignalProducer<[String], NSError>.init(value: [])) { (combined, s) in
            return combined.combineLatestWith(s).map({ (arr, str) in
                return arr + [str]
            })
        }

but it looks ugly

@andersio
Copy link
Member

You can use the concatenation strategy instead, which would enforce the order. However, you should note that the starting of producer would be serialised too. If you need them to happen in parallel, you might need to use merge and somehow reorder them eventually.

SignalProducer(values: [...]).flatten(.Concat).collect()

@lxian
Copy link
Author

lxian commented Aug 29, 2016

@andersio I'm sorry I didn't quit get it. Can you illustrate the flatten approach a bit by some more concrete code ? And I'm using RAC4, should it be flatMap ? Thank you so much !

@andersio
Copy link
Member

andersio commented Aug 29, 2016

SignalProducer(values: [producerA, producerB, producerC]).flatten(.Concat) would create a flattened producer, which starts & forward values of producerB after producerA completes, etc.

flatMap is just a shorthand of map + flatten.

@mdiep mdiep closed this as completed Aug 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants