Last active
May 15, 2020 15:29
-
-
Save moreindirection/d0e613f78fee9ab66e4659f6fa6ab01a to your computer and use it in GitHub Desktop.
Async / Await in Swift
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
func loadWebResource(_ path: String) -> AnyPublisher<Resource, Error> | |
func decodeImage(_ r1: Resource, _ r2: Resource) -> AnyPublisher<Image, Error> | |
func dewarpAndCleanupImage(_ i : Image) -> AnyPublisher<Image, Error> | |
func processImageData1() -> AnyPublisher<Image, Error> { | |
loadWebResource("dataprofile.txt").zip(loadWebResource("imagedata.txt")).flatMap { (dataResource, imageResource) in | |
decodeImage(dataResource, imageResource) | |
} | |
.flatMap { imageTmp in | |
dewarpAndCleanup(imageTmp) | |
} | |
.eraseToAnyPublisher() | |
} |
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
func loadWebResource(filename: String) -> AnyPublisher<Data, Error> { | |
URLSession.shared.dataTask(with: URL(string: BASE_URL + filename)!) { data, response, error in | |
guard error == nil else { | |
promise(.failure(error!)) | |
} | |
promise(.success(data)) | |
}.eraseToAnyPublisher() | |
} |
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
// from https://gist.github.com/lattner/31ed37682ef1576b16bca1432ea9f782 | |
func processImageData2(completionBlock: (result: Image?, error: Error?) -> Void) { | |
loadWebResource("dataprofile.txt") { dataResource, error in | |
guard let dataResource = dataResource else { | |
completionBlock(nil, error) | |
return | |
} | |
loadWebResource("imagedata.dat") { imageResource, error in | |
guard let imageResource = imageResource else { | |
completionBlock(nil, error) | |
return | |
} | |
decodeImage(dataResource, imageResource) { imageTmp, error in | |
guard let imageTmp = imageTmp else { | |
completionBlock(nil, error) | |
return | |
} | |
dewarpAndCleanupImage(imageTmp) { imageResult in | |
guard let imageResult = imageResult else { | |
completionBlock(nil, error) | |
return | |
} | |
completionBlock(imageResult) | |
} | |
} | |
} | |
} | |
} |
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
// from https://gist.github.com/lattner/31ed37682ef1576b16bca1432ea9f782 | |
func loadWebResource(_ path: String) async -> Resource | |
func decodeImage(_ r1: Resource, _ r2: Resource) async -> Image | |
func dewarpAndCleanupImage(_ i : Image) async -> Image | |
func processImageData1() async -> Image { | |
let dataResource = await loadWebResource("dataprofile.txt") | |
let imageResource = await loadWebResource("imagedata.dat") | |
let imageTmp = await decodeImage(dataResource, imageResource) | |
let imageResult = await dewarpAndCleanupImage(imageTmp) | |
return imageResult | |
} |
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
func loadWebResource(filename: String) -> AnyPublisher<Data, Error> { | |
URLSession.shared.dataTaskPublisher(for: URL(string: BASE_URL + filename)!) | |
.eraseToAnyPublisher() | |
} | |
func processImageData1() -> AnyPublisher<Image, Error> { | |
loadWebResource("dataprofile.txt").zip(loadWebResource("imagedata.txt")).flatMap { (dataResource, imageResource) in | |
decodeImage(dataResource, imageResource) | |
} | |
.flatMap { imageTmp in | |
dewarpAndCleanup(imageTmp) | |
} | |
.eraseToAnyPublisher() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment