-
[Swift, Combine] AnyPublisher 를 async/await throws 로 사용해보자Swift 2023. 10. 4. 09:47
https://developer.apple.com/documentation/swift/withcheckedthrowingcontinuation(function:_:)
import Foundation import Combine extension AnyPublisher where Failure: Error { var asyncThrows: Output { get async throws { try await withCheckedThrowingContinuation { continuation in var cancellable: AnyCancellable? cancellable = first() .sink { completion in switch completion { case .finished: break case .failure(let error): continuation.resume(throwing: error) } cancellable?.cancel() } receiveValue: { value in continuation.resume(with: .success(value)) } } } } }
import Foundation import Combine struct UserNetwork { private let network: Networkable private let baseUrl = "https://api.github.com/users/" private var url: (String) -> URL {{ query in URL(string: baseUrl + query)! }} init(network: Networkable = Network()) { self.network = network } func getUser(username: String) -> AnyPublisher<User, Error> { network.request(url: url(username)) } }
func didTapButton() { // AnyPublisher -> async throws Task { let user = try? await userNetwork.getUser(username: "insub4067").asyncThrows print(user) } // AnyPublisher userNetwork.getUser(username: "insub4067") .sink { completion in guard let error = completion.networkError else { return } switch error { case .badResponse: print(error) case .statusCode(let status): print(error, status) case .badUrl: print(error) case .unknown: break } } receiveValue: { user in print(user) } .store(in: &cancellable) }
'Swift' 카테고리의 다른 글
[Swift] DispatchQueue.global() 과 Task.detatched(priority: .background) 의 차이점 (0) 2023.10.24 [Swift] Mirror 써보기 (0) 2023.10.18 [xcode] shortcuts (0) 2023.08.22 [Swift] Generic 을 활용해서 Array 에서 일치하는 Element 찾기 - Extension (0) 2023.08.17 [Design Pattern] Facade Pattern 이란? (0) 2023.08.15