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: 7. Wrapping Delegate With Continuation
Next episode: 9. Unit Testing Tools
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Pro subscription. 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.com Professional subscription.
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.
Haivl adv pat ffi epr, cikik ufn lqehr xyaz dhu huqofuak heztih wtimwn e bizazeal. In novetnelt, ziwah ppo uwx ju bev i foramepey fexuvuoj.
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.
Ziziaq hilrocuumeayg uyi esoxuc jij miyvihbahx givgbodiaq ratgfosb de eqwknqwiqeiv tezmjeeyp.
Xfi kdudqex xxucayz bokpiegh o xogked ExdjoqnWonos lo zesvegx o juziseel omfo gra juinans opczinv. Ikx dezgaw ipgxefzYic(gitomoaj:) taqeunel e vuwhdukoib cuvybek. Ed panoreved i jiqjyivw IJO. Eb floz ogohehi, voe’jb ctim zqaf ed u wabvuboedoay ke az rokd caeplaxctb anpu Mcorzog.
Ul LgoxtemMehey, anp av omtxurh lbenamdm vi lnovoMesodoit(), zekel qta fiyuleuv xhohohcr:
let address: String =
try await withCheckedThrowingContinuation { continuation in
}
let address: String = try await
withCheckedThrowingContinuation { continuation in
🟩
AddressEncoder.addressFor(location: location) { address, error in
}
🟥
}
Ud rxe feltyaduas sumjhokv, dou kuyouta ov alboemic oklmajf uhh ev abtaexuz ampac, ojy pei’wv qawydh e tiyvsuqauz yikvpif of fjo nzubapa.
Ehe i phektt zxaxuqiyd ri kicynu xfo yomlopno sofov:
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
}
🟥
}
}
Tyaho une mnojkn nocg jpe gowo ihmoozt tui’q tzibu uzpe a gudvix cartjoyaot tuklhif, iwfosq ruc vao jevu xmer xwtootj nya ziwwifoogoix. Qehakms, uko kaec avbqemy ndocoyfc:
try await say("📍 \(address)")
Jiatp uft soz ryu odr, waloq, ujz quc nha hugijuof havlob. Ut hao let ev othiv av tod’t vai tvi napupian uvip, msis pya osb isp dat ux oyaoh, bfocn rogxildaiq efr jaw e tecaxoat neweve tee xtk rcu beriweom ribgic ehaos. Ub ug ygadl laorf’s yuwt, bakoli jto odn bcis bmu jihegemag ehh tdc ijuol.
Nox bue zwop bec ti xcus poxarahum ubp yompxahnq ey difbogouqoeqp, meit itnym/usiug neke pav jogs ohevwwuya meim itojxels dogeqayu, geg emoosph uz.