The starter project contains a custom AddressCoder to convert a location into the nearest address. To simulate a callback API, its method addressFor(location:) requires a completion handler. You’ll wrap this in a manual continuation to post the user’s address as a Blabber message.
The starter project contains a custom AddressCoder to convert a location into the nearest address. To simulate a callback API, its method addressFor(location:) requires a completion handler. You’ll wrap this in a manual continuation to post the user’s address as a Blabber message.
Cinema mode
Mark complete
Download course materials
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
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.
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.
You can unlock the rest of this video course, and our entire catalogue of books and videos, with a Kodeco Personal Plan.
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.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.