Skip to content

Instantly share code, notes, and snippets.

@moreindirection
Last active May 15, 2020 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moreindirection/d0e613f78fee9ab66e4659f6fa6ab01a to your computer and use it in GitHub Desktop.
Save moreindirection/d0e613f78fee9ab66e4659f6fa6ab01a to your computer and use it in GitHub Desktop.
Async / Await in Swift
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()
}
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()
}
// 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)
}
}
}
}
}
// 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
}
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