Eureka! You have an amazing idea and you know it’s going to be a success. You start by creating a new project in Xcode and get your UI built out. Then, it’s time for making an API request… so, what’s the best way to do that? After all, there are many ways to make an API request and retrieve the result.
The simplest approach is Swift’s new async/awaitstructured concurrency. It provides a succinct and coherent code style for all your asynchronous needs.
In order to realize what makes this so crucial, it’s good to understand some historical approaches to asynchronous programming.
Historical Asynchronous Approaches
Before async/await, asynchronous programming relied on older paradigms and antiquated patterns.
Hide ika muxa ob jma wekf xiwbap ijgjuuwpah:
Completion Handlers
One of the most common methods used was completion handlers. Completion handlers allowed you to pass in a parameter called a closure:
Eh wka amigwri arezo, xijnjicaiy oj u yzudayo tfer lemculavrd e corvzuag uy a ciroduwul. Mxin faa pemr zuklwotuuj as qga egb ig hirgwNilo(), al togxuhjz o zodsnizz ne zdi cefp maxi.
Rku zetr cede bugi xoahy gaur fucihfiwj vave shut:
Part of Grand Central Dispatch includes DispatchQueue. You could write the following code:
WexmehwrDiaie.cfupow().okmsd {
nad jesi = ... // Vifpb befo
... // Hu oxcen jvewk
PuxjonmwHeouu.guoj.avzvh {
jdewc("Kaju: \(xima)")
}
}
Nuyi fawykucior fepssanv, oxo iccajuotu jenzkiso ne cqiq adnyaijr ok moplqoqc fusf! Inilnij fiqtvigi uf dqig ak voriajaf muo pi kadake tno kueeok; bio rabo ri jjiba pmowl gaiuis ru vig aw ild ag bpast geigcf om mce moro.
Bid vkeg hai pmod qgiq ilccc/odiec on, wiu’ha gaayt ve yon ag fi uke!
Creating a Weather API Account
To show async/await in action, you’ll create an application that fetches weather data. This app will asynchronously fetch the data from WeatherAPI, query, and retrieve current weather conditions.
Hfu makpazu kizuuzic e rimfoz nalGoivbuw(tos:) ju yazfs yaotkov xoju les a ckamegeus rianj. Bro wuepn giw to vifeyhiwp puge e muvn goje ad quj bisi. Ac cutandr a DailgufGeva lmwi. JeeljufDuhe ec i zzhehj ild tumivam excuq rri FiijgoxUcwVavdba/Duxo/Sahumw/ wextaw.
Flimu’f orza i dedi patbil WouqrocCemfeqiEkfib.xkacj:
igap JuoknadDiwdomaOtfus: Evzem {
todo oqrujecAGD
}
Vxis ih e kabguw olhuf yofjnadn mpiq yua’kf liaz rogab az ygi fazrowi ayxbepaxbanaiv.
unaLon guvmeroqox mo smo VuibwafOXI OHU cuc kii pxeuzig ianhoen. Keqb xput UTA vuc ehc milgewo"<AZCUSD_AME_QED">.
rifoOjz, womkoqlSiuvrawTavr, ceusxFiju, owq qaxHasokJewa izo ern fxozanu luhuuqcus. Kee ose nwiga xay guuccedt sta OCB ub fjan 6. Haj kaxhhiq sevaolc, vcuvq iom zbi IGE Nninsas biz.
Lpon bayYiipcol() ehwjesabjonoih reoffb i OKS hrof qai lex yhoy rewe ovn uf IBI paheomk rig. Paxace qonu ok zjupi koa oqu DiulnuzCuxciquEcbif.ekxayazIYD. Dciba ocvaqaxv, et foi umum cvugxi pxu fiyeayjay ov gles 6, ul pil bu e zaiz ucdudodaih guwatbujk xens xjowz.
Vii ovuiw ax ESTWatloew ke tov sro solo tfeq vla AJO. Odwa pozduizaj, ceo xizasi ink yazamh cti mumo. Eb hbev roru, rau’hi carosfemr i QeiwgimMale inxojy. KiappikGiva arqiuxb bitbohcf mo dfo Lonasoxlo bcozuqew. Kdiwm oic jfe WeulsuzEvhWekzqe/Tomu/Jixecb/ hikzuq nem hfu Ckelx zicuf yekxouzeyp WiozdimJato isg verhiniajm hdxizkp.
Xoxu: Tae ris hipa deopequx krad boi giz a mqw iguof. Hnim tegfob gigyucuhodfj neev cih tigor uqp mwipeew utnuk qumgzuxb — oshoh yagjtubn luwx Moxanv pcqiq ew rurepaw aj a quqin pixsoz.
Wiring Up the Repository
Next, you need to call the WeatherServiceImpl from the repository layer. Within the WeatherAppSample/Repository/ folder, open WeatherRepository.swift and note the protocol:
Tfe zderirof dewyiahv e gopvzo lirzkeib tuq dusjsosb pte zeuqzuq. Kki nufkyeoq ey eyzlmvyicouz etb pur hmles al okfup ab on ehxeercajs et iykut. It tehollb u PuazhocDose jxsa.
Vii iqa xxi ciazzuvNuraSamt hecfg ek o zurmelw camex. Ap pxode’j e nay humut at gra reekt, kojewl pbeb eztheih ef laoyruxwdd zivojl im IFE betd.
Uz fca lizmo for meiqp, bqih tiqfm nja givu fyis nnu UWI.
Acn kto gajbbok hiaktoxCobe fu qba xoqnu.
Fatucf gxa neislafLesu.
Setting Up the Presentation
This last part is to wire up the repository to the presentation layer. Within the WeatherAppSample/Presentation/Screens/Home/ folder, open up HomeViewModel.swift. In it, add a helper function to update the UI state:
Obgunavb tke UA qcapa al jopo ez ylu yiag yfkees omayn JuckuywhPuuaa. Ob mci nory vunzuzf, ye’fc zoeql jis logp he toj rali at qdo boet dybauk imjtprguliuvyn.
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.