Skip to content

Instantly share code, notes, and snippets.

@zafarivaev
Last active January 18, 2022 18:53
Show Gist options
  • Save zafarivaev/83cd42e1d856fd8fadcb841f721a1c75 to your computer and use it in GitHub Desktop.
Save zafarivaev/83cd42e1d856fd8fadcb841f721a1c75 to your computer and use it in GitHub Desktop.
import UIKit
import Combine
...
class ViewController: UIViewController {
...
override func viewDidLoad() {
...
getAvatarFromTheServer()
.handleEvents(receiveSubscription: { print("Subscribed", $0)},
receiveOutput: { print("Got image", $0)},
receiveCompletion: { print("Completion", $0)})
.retry(3)
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { completion in
switch completion {
case let .failure(error):
print("Finished with error: \(error)")
case .finished:
print("Finished")
}
}, receiveValue: { [weak self] image in
self?.imageView.image = image
})
.store(in: &cancellables)
}
private func getAvatarFromTheServer() -> AnyPublisher<UIImage, Error> {
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment