Swift: How to perform concurrent API calls using Combine - swift

I am attempting to perform concurrent API calls using the Combine framework. The API calls are set up like so:
First, call an API to get a list of Posts
For each post, call another API to get Comments
I would like to use Combine to chain these two calls together and concurrently so that it returns an array of Post objects with each post containing the comments array.
My attempt:
struct Post: Decodable {
let userId: Int
let id: Int
let title: String
let body: String
var comments: [Comment]?
}
struct Comment: Decodable {
let postId: Int
let id: Int
let name: String
let email: String
let body: String
}
class APIClient: ObservableObject {
#Published var posts = [Post]()
var cancellables = Set<AnyCancellable>()
init() {
getPosts()
}
func getPosts() {
let urlString = "https://jsonplaceholder.typicode.com/posts"
guard let url = URL(string: urlString) else {return}
URLSession.shared.dataTaskPublisher(for: url)
.receive(on: DispatchQueue.main)
.tryMap({ (data, response) -> Data in
guard
let response = response as? HTTPURLResponse,
response.statusCode >= 200 else {
throw URLError(.badServerResponse)
}
return data
})
.decode(type: [Post].self, decoder: JSONDecoder())
.sink { (completion) in
print("Posts completed: \(completion)")
} receiveValue: { (output) in
//Is there a way to chain getComments such that receiveValue would contain Comments??
output.forEach { (post) in
self.getComments(post: post)
}
}
.store(in: &cancellables)
}
func getComments(post: Post) {
let urlString = "https://jsonplaceholder.typicode.com/posts/\(post.id)/comments"
guard let url = URL(string: urlString) else {
return
}
URLSession.shared.dataTaskPublisher(for: url)
.receive(on: DispatchQueue.main)
.tryMap({ (data, response) -> Data in
guard
let response = response as? HTTPURLResponse,
response.statusCode >= 200 else {
throw URLError(.badServerResponse)
}
return data
})
.decode(type: [Comment].self, decoder: JSONDecoder())
.sink { (completion) in
print("Comments completed: \(completion)")
} receiveValue: { (output) in
print("Comment", output)
}
.store(in: &cancellables)
}
}
How do I chain getComments to getPosts so that the output of comments can be received in getPosts? Traditionally using UIKit, I would use DispatchGroup for this task.
Note that I would like to receive just a single Publisher event for posts from the APIClient so that the SwiftUI view is refreshed only once.

Thanks to #matt's post in the comments above, I've adapted the solution in that SO post for my use case above.
Not too sure if it is the best implementation, but it addresses my problem for now.
func getPosts() {
let urlString = "https://jsonplaceholder.typicode.com/posts"
guard let url = URL(string: urlString) else {return}
URLSession.shared.dataTaskPublisher(for: url)
.receive(on: DispatchQueue.main)
.tryMap({ (data, response) -> Data in
guard
let response = response as? HTTPURLResponse,
response.statusCode >= 200 else {
throw URLError(.badServerResponse)
}
return data
})
.decode(type: [Post].self, decoder: JSONDecoder())
.flatMap({ (posts) -> AnyPublisher<Post, Error> in
//Because we return an array of Post in decode(), we need to convert it into an array of publishers but broadcast as 1 publisher
Publishers.Sequence(sequence: posts).eraseToAnyPublisher()
})
.compactMap({ post in
//Loop over each post and map to a Publisher
self.getComments(post: post)
})
.flatMap {$0} //Receives the first element, ie the Post
.collect() //Consolidates into an array of Posts
.sink(receiveCompletion: { (completion) in
print("Completion:", completion)
}, receiveValue: { (posts) in
self.posts = posts
})
.store(in: &cancellables)
}
func getComments(post: Post) -> AnyPublisher<Post, Error>? {
let urlString = "https://jsonplaceholder.typicode.com/posts/\(post.id)/comments"
guard let url = URL(string: urlString) else {
return nil
}
let publisher = URLSession.shared.dataTaskPublisher(for: url)
.receive(on: DispatchQueue.main)
.tryMap({ (data, response) -> Data in
guard
let response = response as? HTTPURLResponse,
response.statusCode >= 200 else {
throw URLError(.badServerResponse)
}
return data
})
.decode(type: [Comment].self, decoder: JSONDecoder())
.tryMap { (comments) -> Post in
var newPost = post
newPost.comments = comments
return newPost
}
.eraseToAnyPublisher()
return publisher
}
Essentially, we will need to return a Publisher from the getComments method so that we can loop over each publisher inside getPosts.

Related

URLSession.shared.dataTaskPublisher receive cancel

trying to fetch some data with dataTaskPublisher. however, constantly receive following log. it works every once in a while and not sure what's the difference. change URL does not make a difference. still only occasionally succeed the request.
Test2: receive subscription: (TryMap)
Test2: request unlimited
Test2: receive cancel
class DataSource: NSObject, ObservableObject {
var networker: Networker = Networker()
func fetch() {
guard let url = URL(string: "https://jsonplaceholder.typicode.com/posts") else {
fatalError("Invalid URL")
}
networker.fetchUrl(url: url)
}
}
class Networker: NSObject, ObservableObject {
var pub: AnyPublisher<Data, Error>? = nil
var sub: Cancellable? = nil
var data: Data? = nil
var response: URLResponse? = nil
func fetchUrl(url: URL) {
guard let url = URL(string: "https://apple.com") else {
return
}
pub = URLSession.shared.dataTaskPublisher(for: url)
.receive(on: DispatchQueue.main)
.tryMap() { data, response in
guard let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200 else {
throw URLError(.badServerResponse)
}
return data
}
.print("Test2")
.eraseToAnyPublisher()
sub = pub?.sink(
receiveCompletion: { completion in
switch completion {
case .finished:
break
case .failure(let error):
fatalError(error.localizedDescription)
}
},
receiveValue: {
print($0)
}
)
}
add .store(in: &subscriptions)

Repeat Network Request Multiple Times With Swift Combine

I'm trying to have a combine pipeline that fetches five random photo urls using the Unsplash API, then downloads a photo from each url. The issue I’m running into is that the images are mostly the same. Sometimes (and I don't know why) one of the images will be different than the other four.
Any help would be appreciated. I've included all the necessary code besides the UnSplash Api key.
static func fetchRandomPhotos() -> AnyPublisher<UIImage, Error>{
let string = createURL(path: "/photos/random")
return [1,2,3,4,5]
.publisher
.flatMap{ i -> AnyPublisher<UnsplashImageResults, Error> in
return self.downloadAndDecode(string, type: UnsplashImageResults.self)
}
.flatMap{ result -> AnyPublisher<UIImage, Error> in
print(result.urls.thumb)
let url = URL(string: result.urls.thumb)!
return URLSession.shared.dataTaskPublisher(for: url)
.map { UIImage(data: $0.data)! }
.mapError{_ in NetworkError.invalidURL}
.eraseToAnyPublisher()
}
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
}
struct UnsplashURLs: Decodable{
let full: String
let regular: String
let small: String
let thumb: String
}
struct UnsplashImageResults: Decodable{
let urls: UnsplashURLs
}
enum NetworkError: LocalizedError{
case invalidURL
var errorDescription: String? {
switch self {
case .invalidURL:
return "The url is invalid"
}
}
}
static private func createURL(path: String)-> String{
var components = URLComponents()
components.scheme = "https"
components.host = "api.unsplash.com"
components.path = path
components.queryItems = [
URLQueryItem(name: "client_id", value: "YOUR API KEY HERE")
]
return components.string!
}
static private func downloadAndDecode<T:Decodable>(_ urlString: String, type: T.Type) -> AnyPublisher<T, Error>{
guard let url = URL(string: urlString) else{
return Fail(error: NetworkError.invalidURL).eraseToAnyPublisher()
}
return URLSession.shared.dataTaskPublisher(for: url)
.tryMap() { element -> Data in
guard let httpResponse = element.response as? HTTPURLResponse,
httpResponse.statusCode == 200 else {
throw URLError(.badServerResponse)
}
return element.data
}
.decode(type: T.self, decoder: JSONDecoder())
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
}
class UnsplashImagesViewModel: ObservableObject{
var subscriptions = Set<AnyCancellable>()
#Published var images = [UIImage]()
init(){
UnsplashAPI.fetchRandomPhotos()
.sink { (_) in
} receiveValue: { image in
self.images.append(image)
}.store(in: &subscriptions)
}
}
struct UnsplashImagesGrid: View {
#StateObject private var model = UnsplashImagesViewModel()
var body: some View {
List(model.images, id: \.self){ image in
Image(uiImage: image)
}
}
}
Unsplash returns the same image, if you send multiple requests at the same time. You can test this behaviour by introducing a delay between your requests.
Anyway, you should be using the count param of Unsplash random API to request a particular number of images. I have made some changes to your code, to receive an array of UIImage. I hope this gives you an idea about, how it can improved further.
static func fetchRandomPhotos() -> AnyPublisher<[UIImage], Error>{
let string = createURL(path: "/photos/random")
return Just(())
.print()
.flatMap{ i -> AnyPublisher<[UnsplashImageResults], Error> in
return self.downloadAndDecode(string, type: UnsplashImageResults.self)
}
.flatMap{ results -> AnyPublisher<[UIImage], Error> in
let images = results.map { result -> AnyPublisher<UIImage, Error> in
let url = URL(string: result.urls.thumb)!
return URLSession.shared.dataTaskPublisher(for: url)
.map { UIImage(data: $0.data)! }
.mapError{_ in NetworkError.invalidURL}
.eraseToAnyPublisher()
}
return Publishers.MergeMany(images)
.collect()
.eraseToAnyPublisher()
}
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
}
static private func createURL(path: String)-> String{
...
components.queryItems = [
URLQueryItem(name: "client_id", value: "ID"),
URLQueryItem(name: "count", value: "5")
]
...
}
static private func downloadAndDecode<T:Decodable>(_ urlString: String, type: T.Type) -> AnyPublisher<[T], Error>{
...
.decode(type: [T].self, decoder: JSONDecoder())
...
}

How could I throw an Error when response data does not contain an object to decode with Combine?

I have a publisher wrapper struct where I can handle response status code. If the status code is not range in 200..300 it return with an object, otherwise it throws an Error. It works well.
public func anyPublisher<T:Decodable>(type: T.Type) -> AnyPublisher<T, Error> {
return URLSession.shared.dataTaskPublisher(for: urlRequest)
.tryMap { output in
guard let httpResponse = output.response as? HTTPURLResponse, 200..<300 ~= httpResponse.statusCode else {
throw APIError.unknown
}
return output.data
}
.decode(type: T.self, decoder: JSONDecoder())
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
Using:
let sendNewUserPublisher = NetworkPublisher(urlRequest: request).anyPublisher(type: User.self)
cancellationToken = sendNewUserPublisher.sink(receiveCompletion: { completion in
if case let .failure(error) = completion {
NSLog("error: \(error.localizedDescription)")
}
}, receiveValue: { post in
self.post = post
})
As above, I would like to handle the error even if the response data does not contain an object to be decoded.
public func anyPublisher() -> AnyPublisher<URLSession.DataTaskPublisher.Output, URLSession.DataTaskPublisher.Failure> {
return URLSession.shared.dataTaskPublisher(for: urlRequest)
// I'd like to handle status code here, and throw an error, if needed
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
Thank you in advance for any help you can provide.
I would suggest creating a Publisher that handles the HTTP response status code validation and using that for both of your other publishers - the one that handles an empty request body and the one that decodes the body.
If you need the HTTPURLResponse object even after validating its status code:
extension URLSession.DataTaskPublisher {
/// Publisher that throws an error in case the data task finished with an invalid status code, otherwise it simply returns the body and response of the HTTP request
func httpResponseValidator() -> AnyPublisher<Output, CustomError> {
tryMap { data, response in
guard let httpResponse = response as? HTTPURLResponse else { throw CustomError.nonHTTPResponse }
let statusCode = httpResponse.statusCode
guard (200..<300).contains(statusCode) else { throw CustomError.incorrectStatusCode(statusCode) }
return (data, httpResponse)
}
.mapError { CustomError.network($0) }
.eraseToAnyPublisher()
}
}
Or if you don't care about any other properties of the response, only that its status code was valid:
func httpResponseValidator() -> AnyPublisher<Data, CustomError> {
tryMap { data, response in
guard let httpResponse = response as? HTTPURLResponse else { throw CustomError.nonHTTPResponse }
let statusCode = httpResponse.statusCode
guard (200..<300).contains(statusCode) else { throw CustomError.incorrectStatusCode(statusCode) }
return data
}
.mapError { CustomError.network($0) }
.eraseToAnyPublisher()
}
Then you can use this to rewrite both versions of your anyPublisher function:
extension URLSession.DataTaskPublisher {
func anyPublisher<T:Decodable>(type: T.Type) -> AnyPublisher<T, Error> {
httpResponseValidator()
.decode(type: T.self, decoder: JSONDecoder())
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
func anyPublisher() -> AnyPublisher<Output, CustomError> {
httpResponseValidator()
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
}

handling HTTP status code with URLSession and Combine

I'm trying to handle the responses that arrive from a DataTaskPublisher reading its response status code.
When status code is greater than 299, I'd like to return a ServiceError type as Failure. In every examples that I've seen I've used .mapError and .catch... in this specific case, from a .flatMap, I really don't know how to handle the publisher response to return the Error instead of the TResponse...
return URLSession.DataTaskPublisher(request: urlRequest, session: .shared)
.mapError{error in return ServiceError.request}
.flatMap{ data, response -> AnyPublisher<TResponse, ServiceError> in
if let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode){
return Just(data)
.decode(type: TResponse.self, decoder: JSONDecoder())
.mapError{error in return ServiceError.decode}
.eraseToAnyPublisher()
}else{
//???? HOW TO HANDLE THE ERROR?
}
}
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
enum ServiceErrors: Error {
case internalError(_ statusCode: Int)
case serverError(_ statusCode: Int)
}
return URLSession.shared.dataTaskPublisher(for: urlRequest)
.tryMap { data, response in
guard let httpResponse = response as? HTTPURLResponse,
200..<300 ~= httpResponse.statusCode else {
switch (response as! HTTPURLResponse).statusCode {
case (400...499):
throw ServiceErrors.internalError((response as! HTTPURLResponse).statusCode)
default:
throw ServiceErrors.serverError((response as! HTTPURLResponse).statusCode)
}
}
return data
}
.mapError { $0 as! ServiceErrors }
.decode(type: T.self, decoder: JSONDecoder())
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
NOTE: I relied on this link to make my error handler.
If I correctly understood your goal, you need something like
}else{
return Fail(error: ServiceError.badServiceReply)
.eraseToAnyPublisher()
}
Simple example:
URLSession.shared
.dataTaskPublisher(for: URL(string: "https://www.google.com")!)
.receive(on: DispatchQueue.main)
.flatMap { _ in
Fail(error: URLError(URLError.unsupportedURL)).eraseToAnyPublisher()
} //for the sake of the demo
.replaceError(with: "An error occurred") //this sets Failure to Never
.assign(to: \.stringValue, on: self)
.store(in: &cancellableBag)
would always assign string "An error occurred" due to remap to Fail publisher
You can use tryMap here:
URLSession.shared.dataTaskPublisher(for: urlRequest)
.mapError { error in return ServiceError.request }
.tryMap { (data, response) throws -> TResponse in
guard let httpResponse = response as? HTTPURLResponse,
200...299 ~= httpResponse.statusCode else {
throw ServiceError.invalidStatusCode((response as? HTTPURLResponse)?.statusCode ?? 0)
}
return try JSONDecoder().decode(TResponse.self, from: data)
}
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
This will also simplify your code as you don't need do create an extra publisher.
I also made a few other improvements to the original code:
used URLSession.shared.dataTaskPublisher(for: urlRequest) instead of URLSession.DataTaskPublisher(request: urlRequest, session: .shared)
used the ~= operator to check the status code
used a guard instead of a let as it's more idiomatic

How can I loop over the output of a publisher with Combine?

I'm working on rewriting my Hacker News reader to use Combine more heavily. I'm have two functions which both return an AnyPublisher, one of them get's the ids of a bunch of HN stories from the server and the other one fetches a story by it's id. I'm not sure how I could loop over the results of fetchStoryIds, run fetchStory with the id and end up with an array of Story objects with Combine.
import Combine
import Foundation
struct HackerNewsService {
private var session = URLSession(configuration: .default)
static private var baseURL = "https://hacker-news.firebaseio.com/v0"
private func fetchStoryIds(feed: FeedType) -> AnyPublisher<[Int], Error> {
let url = URL(string: "\(HackerNewsService.baseURL)/\(feed.rawValue.lowercased())stories.json")!
return session.dataTaskPublisher(for: url)
.retry(1)
.map { $0.data }
.decode(type: [Int].self, decoder: JSONDecoder())
.eraseToAnyPublisher()
}
private func fetchStory(id: Int) -> AnyPublisher<Story, Error> {
let url = URL(string: "\(HackerNewsService.baseURL)/item/\(id).json")!
return session.dataTaskPublisher(for: url)
.map { $0.data }
.decode(type: Story.self, decoder: JSONDecoder())
.eraseToAnyPublisher()
}
}
Before I started the rewrite, I used this code to loop over the ids and get the stories.
func fetchStories(feed: FeedType, completionHandler: #escaping ([Story]?, Error?) -> Void) {
fetchStoryIds(feed: feed) { (ids, error) in
guard error == nil else {
completionHandler(nil, error)
return
}
guard let ids = ids else {
completionHandler(nil, error)
return
}
let dispatchGroup = DispatchGroup()
var stories = [Story]()
for id in ids {
dispatchGroup.enter()
self.fetchStory(id: id) { (story, error) in
guard error == nil else {
dispatchGroup.leave()
return
}
guard let story = story else {
dispatchGroup.leave()
return
}
stories.append(story)
dispatchGroup.leave()
}
}
dispatchGroup.notify(queue: .main) {
completionHandler(stories, nil)
}
}
}
}
Hmm.. It doesn't look like there is a Publishers.ZipMany that accepts a collection of publishers, so instead I merged the stories and collected them instead. Ideally this would collect them in the correct order but I haven't tested that and the documentation is still somewhat sparse across Combine.
func fetchStories(feed: FeedType) -> AnyPublisher<[Story], Error> {
fetchStoryIds(feed: feed)
.flatMap { ids -> AnyPublisher<[Story], Error> in
let stories = ids.map { self.fetchStory(id: $0) }
return Publishers.MergeMany(stories)
.collect()
.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}
If you are open to external code this is a gist implementation of ZipMany that will preserve the order:
https://gist.github.com/mwahlig/725fe5e78e385093ba53e6f89028a41c
Although I would think that such a thing would exist in the framework.