in

Combine, retrieve array of images from array of URLs and assign them to corresponding object


I am quite new using Combine and frankly I don’t know whether this question is silly or not, anyway I have a publisher which I then return to the caller with an array of objects retrieved from a RESTful operation in the following way:

let publisher = URLSession.shared.dataTaskPublisher(for: URL)
            .handleEvents(
                receiveSubscription: { _ in
                    activityIndicatorPublisher.send(true)
                }, receiveCompletion: { _ in
                    activityIndicatorPublisher.send(false)
                }, receiveCancel: {
                    activityIndicatorPublisher.send(false)
                })
            .tryMap { data, response -> Data in
                guard let httpResponse = response as? HTTPURLResponse,
                      httpResponse.statusCode == 200 else {
                          throw NetworkError.httpError
                      }
                print(String(data: data, encoding: .utf8) ?? "")
                return data
            }
            .decode(type: [ShowElement].self, decoder: JSONDecoder())
            .map { $0 }
            .catch { err -> Just<[ShowElement]> in
                print(err)
                return Just([])
            }
            .eraseToAnyPublisher()
        return publisher

One of the properties of my objects is an URL to download an image, I have prepared a Data? var property for the downloaded image but I can’t figure out how to integrate the download in this pipeline, I mean it would be cycling every object in {0} retrieve the URL and then asynchronously download each image ad assign the data to the appropriate object property. Is this even feasible in a single pipeline?



Source: https://stackoverflow.com/questions/70720545/combine-retrieve-array-of-images-from-array-of-urls-and-assign-them-to-correspo

DevKit — The ultimate developer toolkit

Bharti Airtel Prepaid Plans With OTT Benefits Under Rs 1000