So notwithstanding my comment about the context for this potentially mattering a great deal, here's how I would asynchronously convert [A]
to [B]
using ReactiveSwift. Note that I haven't had a chance to test this code, but it should get across the basic idea:
// This function takes an `NSURL` and creates an asynchronous `SignalProducer` to
// download an image from that URL or yields `nil` if there is an error.
func downloadImage(url: URL) -> SignalProducer<UIImage?, NoError> {
let request = URLRequest(url: url)
return URLSession.shared.reactive.data(with: request)
.map { (data, response) -> UIImage? in UIImage(data: data) }
.flatMapError { _ in SignalProducer<UIImage?, NoError>(value: nil) }
}
func convertAToB(_ a: A) -> SignalProducer<B, NoError> {
let imgDownload = downloadImage(url: a.imageURL)
let thumbDownload = downloadImage(url: a.thumbnailURL)
return SignalProducer<UIImage?, NoError>.combineLatest(imgDownload, thumbDownload)
.map { images in
return B(name: a.name, image: images.0, thumbnail: images.1)
}
}
func convertAllAsToBs(_ inputs: [A]) -> SignalProducer<[B], NoError> {
return SignalProducer<A, NoError>(values: inputs)
.flatMap(.concat, convertAToB)
.collect()
}
let inputs: [A] = ...
convertAllAsToBs(inputs).startWithValues { outputs in
// `outputs` is [B]
}
Edit:
To bring this answer to parity with @PhilippeC's RxSwift answer, here's a summary of what each ReactiveSwift operator is doing:
SignalProducer.init(values:)
is the equivalent of RxSwift's Observable.from
. It creates a producer that sends each value of the sequence as a separate event.
collect
is the equivalent of RxSwift's toArray
. It collects each value from the source producer and sends them along in a single array once the source producer completes.
flatMap
starts the convertAToB
producer for each incoming A
, and merges the result according to to specified FlattenStrategy
. In this case, I used .concat
, which makes it equivalent to RxSwift's concatMap
which concatenates each result and preserves the order as @PhilippeC describes in his answer.