Last active
January 19, 2022 17:12
-
-
Save zafarivaev/41c4fc535b641bea59a402d71fda8c2b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 UIKit | |
import Combine | |
... | |
class ViewController: UIViewController { | |
... | |
override func viewDidLoad() { | |
... | |
} | |
private func getAvatarFromTheServer() -> AnyPublisher<UIImage, Error> { | |
// 1 | |
let url = URL(string: "https://picsum.photos/1000")! | |
// 2 | |
return Deferred { | |
Future { promise in | |
DispatchQueue.global().async { | |
guard let data = try? Data(contentsOf: url), | |
let image = UIImage(data: data) else { | |
// 3 | |
promise(.failure(CustomError.dataCorrupted)) | |
return | |
} | |
// 4 | |
promise(.success(image)) | |
} | |
} | |
}.eraseToAnyPublisher() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment