I have two lists of URLs that return some links to images. The lists are passed into a future like
static func loadRecentEpisodeImagesFuture(request: [URL]) -> AnyPublisher<[RecentEpisodeImages], Never> { return Future { promise in print(request) networkAPI.recentEpisodeImages(url: request) .sink(receiveCompletion: { _ in }, receiveValue: { recentEpisodeImages in promise(.success(recentEpisodeImages)) }) .store(in: &recentImagesSubscription) } .eraseToAnyPublisher() } Which calls:
/// Get a list of image sizes associated with a featured episode . func featuredEpisodeImages(featuredUrl: [URL]) -> AnyPublisher<[FeaturedEpisodeImages], Error> { let featuredEpisodesImages = featuredUrl.map { (featuredUrl) -> AnyPublisher<FeaturedEpisodeImages, Error> in return URLSession.shared .dataTaskPublisher(for: featuredUrl) .map(\.data) .decode(type: FeaturedEpisodeImages.self, decoder: decoder) .receive(on: networkApiQueue) .catch { _ in Empty<FeaturedEpisodeImages, Error>() } .print("###Featured###") .eraseToAnyPublisher() } return Publishers.MergeMany(featuredEpisodesImages).collect().eraseToAnyPublisher() } /// Get a list of image sizes associated with a recent episode . func recentEpisodeImages(recentUrl: [URL]) -> AnyPublisher<[RecentEpisodeImages], Error> { let recentEpisodesImages = recentUrl.map { (recentUrl) -> AnyPublisher<RecentEpisodeImages, Error> in return URLSession.shared .dataTaskPublisher(for: recentUrl) .map(\.data) .decode(type: RecentEpisodeImages.self, decoder: decoder) .receive(on: networkApiQueue) .catch { _ in Empty<RecentEpisodeImages, Error>() } .print("###Recent###") .eraseToAnyPublisher() } return Publishers.MergeMany(recentEpisodesImages).collect().eraseToAnyPublisher() } and is attached to the app state:
/// Takes an action and returns a future mapped to another action. static func recentEpisodeImages(action: RequestRecentEpisodeImages) -> AnyPublisher<Action, Never> { return loadRecentEpisodeImagesFuture(request: action.request) .receive(on: networkApiQueue) .map({ images in ResponseRecentEpisodeImages(response: images) }) .replaceError(with: RequestFailed()) .eraseToAnyPublisher() } It seems that:
return Publishers.MergeMany(recentEpisodes).collect().eraseToAnyPublisher() doesn't give me a reliable downstream value as whichever response finishes last overwrites the earlier response.
I am able to log the responses of both series of requests. Both are processing the correct arrays and returning the proper json.
I would like something like:
return recentEpisodeImages but currently this gives me the error
Cannot convert return expression of type '[AnyPublisher<RecentEpisodeImages, Error>]' to return type 'AnyPublisher<[RecentEpisodeImages], Error>'
How can I collect the values of the inner publisher and return them as
AnyPublisher<[RecentEpisodeImages], Error>
[URL]. From what I think I understand about URLSession I am getting back[AnyPublisher<RecentEpisodeImages, Error>]fromrecentUrl.mapbut I need needAnyPublisher<[RecentEpisodeImages], Error>