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())
.flatMap { $0.publisher }
.map(\.show?.image?.medium)
.flatMap(maxPublishers:.max(1)) { url in
URLSession.shared.dataTaskPublisher(for: url)
.map(\.data)
.replaceError(with: Data())
}
.map{ imageData, item -> ShowElement in
var mutableItem = item
mutableItem.imageData = imageData
return mutableItem
}
.collect()
.catch { error -> Just<[ShowElement]> in
print(error)
return Just([])
}
.eraseToAnyPublisher()
return publisher
}
When I do the dataTaskPublisher I receive the error: "No exact match in call to instance methode dataTaskPublisher, and when I do the mapping of the imageData I receive another error: "Contextual closure type '(Publishers.FlatMap<Publishers.SetFailureType<Publishers.ReplaceError<Publishers.MapKeyPath<URLSession.DataTaskPublisher, Data>>, Error>, Publishers.MapKeyPath<Publishers.FlatMap<Publishers.SetFailureType<Publishers.Sequence<[ShowElement], Never>, Error>, Publishers.Decode<Publishers.TryMap<Publishers.HandleEvents<URLSession.DataTaskPublisher>, JSONDecoder.Input>, [ShowElement], JSONDecoder>>, String?>>.Output) -> ShowElement' (aka '(Data) -> ShowElement') expects 1 argument, but 2 were used in closure body"