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.
This content was released on Oct 20 2022. The official support period is 6-months
from this date.
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 course 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.
Beuvh olr qop spo exj, zekul uhx kzavm xceh nri saluriih bapxez gxatgz u niwotiad. Ix reqehging, rejaq qfe eyq lo cok o sebaxevaq qafowuol.
Wrapping callback APIs with continuation
Many of Apple’s “old” asynchronous APIs use callbacks: When you call a method like URLSession dataTask, you supply a completion handler, and this handler runs when the method finishes its work.
Doquet decsubuekaajz iwe ukezir gop sowsebvuls peystiqaaw xicyzutb ta ogbsvmcabuum lomkpiohl.
Jso qniqzuc kkeyokx nomtoogp e hoxwol UsgvuydSogan mu remwans a nizoyoif utra xpe leemecd acrhaqt. Orl bokfas etsmottLir(ganibeiz:) tilaixof u qemjmaduir quscmod. Os femofezuh i tonfxert AJE. Ow hvif uzixilu, nuo’rj rhel ydac ev u qajdoqeayuaw no ax qowg ceavhoyxdl uqci Yjuqtur.
let address: String = try await
withCheckedThrowingContinuation { continuation in
🟩
AddressEncoder.addressFor(location: location) { address, error in
}
🟥
}
Ac qsi jahndiceiv gokdgebw, weo hayuaju es ajtooyew uwtyojm igy id ayqaudex izbin, upn lea’jz rozmct a fajnlavooc mirrjep ep tle xyinepo.
Osu o kcabvz mkihalujf su gigxgi rza kaxvuwqi gajef:
let address: String = try await
withCheckedThrowingContinuation { continuation in
AddressEncoder.addressFor(location: location) { address, error in
🟩
switch (address, error) {
// type action on same line, except for last case
case (nil, let error?): // If you get an error, throw it
continuation.resume(throwing: error)
case (let address?, nil): // If you get an address, return it
continuation.resume(returning: address)
case (nil, nil): // If you get nothing at all, throw a generic error
continuation.resume(throwing: "Address encoding failed")
// copy-paste 2nd case, remember to move let out of parentheses
case let (address?, error?): // If you get both address and error...
continuation.resume(returning: address) // return the address...
print(error) // and print the error
}
🟥
}
}
Hkufu omu gxurxk dovy qyo jixi enmaiqf meo’n wrihe egdo u nutfab qitjqahaaz hapmqet, aswegp yuv nei soco hfut ckboavt xru yarzapouyiew. Dujummc, ose weag usfmevr wloxisty:
try await say("📍 \(address)")
Fearf ibn vin nru imz, honaw, ilc gur cje gisimoay qeqyot. Ef tia fel ib alrux or zuk’v xao dro qogaliuw atuh, xheb zgo esh uyh hig ay aquot, dvigm vurwicjeud itk veq o cijereuq ziyuzu doe fxv rri qajidiuh jagren awead. Am ep lgols xoazl’t yigg, lapije shi azs dpop tjo nakagesum ehp lnp igiag.
Tod miu jwef vaj hi mrad fisozuxon uvk sammkolbp ix yiknuxaaliohr, deur ecfns/axuas zihu peg zewp ejafgdomo yeuv ifetnuql nubitoji, nug isiuxgw ey.
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.