Last active
January 16, 2021 08:23
-
-
Save kublaios/0ec48aeb0ff353a8ad59747f52f5ea75 to your computer and use it in GitHub Desktop.
Swift async/await demonstration
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 UIKit | |
import _Concurrency | |
class SendSelectedPhotoWithEmailViewController: UIViewController { | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
// self.startFlow_PoD() | |
self.startFlow_AsyncAwait() | |
} | |
func startFlow_PoD() { | |
PhotosAPI.grantPhotosAccess { didGrantAccess in | |
if didGrantAccess { | |
PhotosAPI.selectPhoto { selectedPhoto in | |
NetworkAPI.upload(image: selectedPhoto) { uploadedFileUrl in | |
NetworkAPI.sendUrlWithEmail(urlString: uploadedFileUrl) { [weak self] didSend in | |
self?.displayResultAlert(didSendEmail: didSend) | |
} | |
} | |
} | |
} | |
} | |
} | |
@asyncHandler func startFlow_AsyncAwait() { | |
guard await PhotosAPI.grantPhotosAccess() else { return } | |
let selectedPhoto = await PhotosAPI.selectPhoto() | |
let uploadedFileUrl = await NetworkAPI.upload(image: selectedPhoto) | |
let didSendUrlWithEmail = await NetworkAPI.sendUrlWithEmail(urlString: uploadedFileUrl) | |
self.displayResultAlert(didSendEmail: didSendUrlWithEmail) | |
} | |
private func displayResultAlert(didSendEmail: Bool) { | |
// Display alert to the user | |
} | |
} | |
class PhotosAPI { | |
// MARK: Old PhotosAPI code (closures) | |
static func grantPhotosAccess(completion: @escaping (Bool) -> Void) { | |
DispatchQueue.main.async { | |
sleep(1) // Fake wait | |
completion(true) | |
} | |
} | |
static func selectPhoto(completion: @escaping (UIImage) -> Void) { | |
DispatchQueue.main.async { | |
sleep(1) // Fake wait | |
completion(UIImage()) | |
} | |
} | |
// MARK: New PhotosAPI code (async/await) | |
static func grantPhotosAccess() async -> Bool { | |
await withUnsafeContinuation { continuation in | |
DispatchQueue.main.async { | |
sleep(1) // Fake wait | |
continuation.resume(returning: true) | |
} | |
} | |
} | |
static func selectPhoto() async -> UIImage { | |
await withUnsafeContinuation { continuation in | |
DispatchQueue.main.async { | |
sleep(1) // Fake wait | |
continuation.resume(returning: UIImage()) | |
} | |
} | |
} | |
} | |
class NetworkAPI { | |
// MARK: Old NetworkAPI code (closures) | |
static func sendUrlWithEmail(urlString: String, completion: @escaping (Bool) -> Void) { | |
DispatchQueue.main.async { | |
sleep(1) // Fake wait | |
completion(true) | |
} | |
} | |
static func upload(image: UIImage, completion: @escaping (String) -> Void) { | |
DispatchQueue.main.async { | |
sleep(1) // Fake wait | |
completion("fake URL") | |
} | |
} | |
// MARK: New NetworkAPI code (async/await) | |
static func sendUrlWithEmail(urlString: String) async -> Bool { | |
await withUnsafeContinuation { continuation in | |
DispatchQueue.main.async { | |
sleep(1) // Fake wait | |
continuation.resume(returning: true) | |
} | |
} | |
} | |
static func upload(image: UIImage) async -> String { | |
await withUnsafeContinuation { continuation in | |
DispatchQueue.main.async { | |
sleep(1) // Fake wait | |
continuation.resume(returning: "fake URL") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment