Skip to content

Instantly share code, notes, and snippets.

@zafarivaev
Last active January 19, 2022 17:12
Show Gist options
  • Save zafarivaev/41c4fc535b641bea59a402d71fda8c2b to your computer and use it in GitHub Desktop.
Save zafarivaev/41c4fc535b641bea59a402d71fda8c2b to your computer and use it in GitHub Desktop.
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