Splitting arrays the Reactive Cocoa way
If you, like me, find yourself working with Rest API:s there is also a big chance you work alot with arrays of data. Usually you also want to do some work on each item in the array. The traditional way would be to loop over the data. We want to split it up and stream it down a pipe as individual elements, the reactive way.
As it turns out, Reactive Cocoa has support for splitting up arrays and send each item down the pipe. Enter SignalProducer(values: _)
. It will instantiate a SignalProducer which will send all items in the array and then complete. Just what we are looking for.
Let´s have a look at a basic example. We have some input in the form of an array. The array is split up, some work is performed on each element. Finally the array is assembled again.
Ok, so that is good enough. It does exactly what it is supposed to.
I found myself reimplementing this snippet over and over. Maybe we can DRY it up a bit?
Let´s implement SignalProducer(values: _)
as a function on the SignalProducer.
We add a function which will work on SignalProducers which produces SequenceTypes (i.e. Arrays). The function will return a SignalProducer which produces items of the same type as found in the SequenceType. In our case it means we are operating on a SignalProducer<[String], Error>
, which in turn will produce a SignalProducer<String, Error>
.
Finally the original code, but with the new SignalProducer.values()
function instead.
Hope you enjoyed this article and as always, if you have any feedback I would be glad to hear it.