Last active
January 28, 2023 18:07
-
-
Save stinger/7cb1a81facf7f846e3d53f60be34dd1e to your computer and use it in GitHub Desktop.
Combine - fetching data using URLSession publishers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Combine | |
enum APIError: Error, LocalizedError { | |
case unknown, apiError(reason: String) | |
var errorDescription: String? { | |
switch self { | |
case .unknown: | |
return "Unknown error" | |
case .apiError(let reason): | |
return reason | |
} | |
} | |
} | |
func fetch(url: URL) -> AnyPublisher<Data, APIError> { | |
let request = URLRequest(url: url) | |
return URLSession.DataTaskPublisher(request: request, session: .shared) | |
.tryMap { data, response in | |
guard let httpResponse = response as? HTTPURLResponse, 200..<300 ~= httpResponse.statusCode else { | |
throw APIError.unknown | |
} | |
return data | |
} | |
.mapError { error in | |
if let error = error as? APIError { | |
return error | |
} else { | |
return APIError.apiError(reason: error.localizedDescription) | |
} | |
} | |
.eraseToAnyPublisher() | |
} | |
// Usage | |
guard let url = URL(string: "https://www.amazon.com") else { return } | |
fetch(url: url) | |
.sink(receiveCompletion: { completion in | |
switch completion { | |
case .finished: | |
break | |
case .failure(let error): | |
print(error.localizedDescription) | |
} | |
}, receiveValue: { data in | |
guard let response = String(data: data, encoding: .utf8) else { return } | |
print(response) | |
}) |
It does not work when you encapsulate the fetch inside a function and have the Cancellable let sub
reside on the same scope because the completion is called long after the function is destroyed!
It is necessary to have a global Cancellable optional variable available that survives the entire scope of the caller of fetch!
@junweimah The reason is that
sink
is returning an Cancellable. If this cancellable is not referenced it gets deallocated immediately and is calling cancel. If you hold a reference to it this is not the case.let sub = fetch(url: url) .sink(receiveCompletion: { completion in switch completion { case .finished: break case .failure(let error): print(error.localizedDescription) } }, receiveValue: { data in guard let response = String(data: data, encoding: .utf8) else { return } print(response) })Should work here
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@junweimah The reason is that
sink
is returning an Cancellable. If this cancellable is not referenced it gets deallocated immediately and is calling cancel. If you hold a reference to it this is not the case.Should work here