Modern Concurrency: Beyond the Basics

Oct 20 2022 · Swift 5.5, iOS 15, Xcode 13.4

Part 1: AsyncStream & Continuations

08. Wrapping Callback With Continuation

Episode complete

Play next episode

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 07. Wrapping Delegate With Continuation Next episode: 09. Unit Testing Tools

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 08. Wrapping Callback With Continuation

At the time of recording, Xcode 14 flags a runtime error in the location delegate continuation. If this happens to you, use Xcode 13 for the rest of Part 1.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Refresh your browser to make sure the core server is running, or restart the server in terminal. Continue with your project from the previous episode, or open the starter project for this episode. Build and run the app. Log in and check that the location button prints the location. If necessary, re-run the app to set a simulated location. Many of Apple's old asynchronous APIs use callbacks. When you call a method like url session data task, you supply a completion handler, and this handler runs when the method finishes its work. Manual continuations are useful for converting completion handlers to asynchronous functions. The starter project contains a custom address coder to convert a location into the nearest address. Its method, address for location, requires a completion handler. It simulates a callback API. In this episode, you'll wrap this in a continuation so it fits seamlessly into Blabber. In Blabber Model, add an address property to share location below the location property. This is like what you did when you wrapped the location manager delegate. This time, you'll return a string, the human-friendly address for the location coordinates. Inside the closure, call address for location. In the completion callback, you receive an optional address and an optional error, and you'll supply a completion handler in the closure. Use a switch statement to handle the possible cases. If you get an error, throw it. If you get an address, return it. If you get nothing at all, throw a generic error. If you get both address and error, return the address... And print the error. These are pretty much the same actions you would write into a normal completion handler, except now, you pipe them through the continuation. Finally, use your address property. Build and run the app. Log in. Then tap the location button. If you get an error, or you can't see the location icon, stop the app and run it again. Grant permission and set a location before you try the location button again. If it still doesn't work, delete the app from the simulator and try again. Now you know how to wrap delegates and callbacks in continuations. Your a await code can work alongside your existing code base, not against it. In the next episode, you'll learn more about debugging and testing your asynchronous code.