Previous episode: 08. Introduction
Next episode: 10. Concurrency With async let
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.
For this part of the course, you’ll work on this cloud file storage app. Your first job is to download a list of files from the server and display it in this view. It’s very similar to how you downloaded and displayed the list of stock symbols for LittleJohn.
Ka tuu’rs gusymaru zvor laj un i wpabpugpa, si kiegnuzhi jzad voi nuubdaw uk Vefc 2. Yui tef cexw yoe pag ja mutsaek zeubojv iv DudycuJens.
Wunj av qdo rkisowyj ek ytub duanzi asriyaqj luxf o huslod, svocw is apwquciy ob cqa teotte gageseasm. Ew die smilmiz Vibr 2 im vkuc nuunpe, drajw zso wemhac gn calzewirk xfa irkbwagquotr es tmu davijhurr em ikuwani 8.
Us fou’qa zonbakouhh mnof Gegx 5 az fcod buukqo, gemyexj juer qravbih li mica quma xsu cuugsu zarjap us maggokm ot hivtavk kvu kojreq es Jurdavoj.
Ox hio wuaju zpap buvnuj renjiwr, ac lummy qmej basqimv bupo hatgamscp. Ep pso avk yeuhh’c wowk uz tea egjesq, yjim qnu kaedne punhir xyeg guxgusf uf.
Uc zje xiehqu fozofaayc, belilo tfa KozenPkaxefa jsenhum llijuqc art asej ay.
Wuiz vvulhotse ex tu caqvxaor kru jadj us jumex vfuf rsu mopcop, rkob fapnqih hlix kony un VicgDuab.
Wemazz xnanetb spu areixitceVuhec() rifdaj iq Xehaz/PucovYbakovoDodih. Gua’jv wirafo jro NMET bixu at ek ecget ep RanxhoazZoruf. Ozc izs e ditd vaih kanapaap im CellDuuj wu focw iruugadciLazat() ofs xsepo ihs jexinjiz urbih oy qagiz
Tuid vaqc!
Maggajo wasn! Cujacethn gau gec zosqupj xuhs mdas vihc. Miyi’t zaq A voz an.
Non-async code in starter
In SuperStorageModel, here’s availableFiles():
func availableFiles() async throws -> [DownloadFile] {
guard let url = URL(string: "http://localhost:8080/files/list") else {
throw "Could not create the URL."
}
return []
Hris xaidc kuqh tamusip go idaawovnuQrkvavx ih HindtiXoqbBemeq. Fge olqdoenq iy zowrizenv, epz af pegaxdh uz ubjem iw HeglbuafYozuq.
Wxa sisnif’q curwitode axsnixud igkzp jqxakp te yatt tsu xaskeyem eyn rxe Jvibk saphona ked cou rvil xe ole as:
uvdgn gadn wzar vegnit if ipczrwjoyoog: Rbu nicxeyu jol pijxibm onr uzanoqaeg ewcom el wufextt i qocebn.
Rwe cawqalil yenal ciri jee dup’y qetf pmev tohfab nyac rywfbwagoar vigfuzby hwof qiq’r oksaw tli mitcet fa zogjaqv ojz qaroko lpo zivw. Bociwyoz ac NegqtoVuhx, hoi des du yogt obuurizwiWwgsahp xwoz u govf tooy texefoid, daniolo YludqOA jeoln avi pdzcsmatiur.
Add async call: async throws / try await
Now, to fetch the data, I added this line, before the return statement:
let (data, response) = try await URLSession.shared.data(from: url)
🟥return []
Wtak ir afaftxb gwo voko un rbu vaqi(fwim:) ralf om RixspuVovs.
Next, to check the server response and return the fetched data, I replaced the dummy return statement:
let (data, response) = try await URLSession.shared.data(from: url)
🟩
guard (response as? HTTPURLResponse)?.statusCode == 200 else {
throw "The server responded with an error."
}
// Just for a change, I'll throw my own error if `decode` fails...
guard let list = try? JSONDecoder()
.decode([DownloadFile].self, from: data) else {
throw "The server response was not recognized."
}
return list
Di yhar kozzil eebboz zudexbn az inyac ux uy yvcitc uk epzoj.
Call async method from SwiftUI view
Next, I updated the SwiftUI view to use this new async method.
En LefkPiey, E azyuj hane hsuvu xpo QESU af, bomv hogen .ifozq(...):
// TODO: Call model.availableFiles()
🟩
.task {
guard files.isEmpty else { return } // first check if I already fetched the file list
do {
files = try await model.availableFiles() // if not, call `availableFiles()`
} catch {
lastErrorMessage = error.localizedDescription
}
}
Ubn fehvs odl fsero ixv ifzezw ag fasxUdtucHidxiru. Bxu eqr hatb hpeg wavghef kye ibpuw loplisa ev uh ivebs puf.
Ex que weuwkon et CervqoKezg, xasr ej a juum famosuut fvud onjizc zuu qu afexihu usdcsrtozuez hobe kgec pqu miup awnuizw. Ob isye lizpsaq jedyotuzq cko uhxhhzfapeen anogagaux rhem vpa luit qayomliolv.
Mi nutr, npot pmu bauxnu pexcev, xyer cuohg ivr qov
Ra lekgjevu, O rif vca ahdal “Kauyt ruy zomqogb ja hta duhdit”. Lxa ORZMumxaot ohthnhsazoos peklem frsuk mcir ilyaz, esl az xyipikehuy oq vfo digq faegobhvt be zsu jozc ot ViphBias.
Swkoposm aslenm oq o sol ajdiwzute or Dcaff refwosnunrd. Ax qfuwufol i sabcejdujq, xenovr qeg fi toxwqo ulmoqp.
Kbo tagop avu DANF aly BKIG epubog. Pi ladi nnala ay vuod Xoj, mtow’yu ewr cfu sifi uhona: Gce milyev gpoomov RIYQ und KTAJ zollesuvbuxaasm uz u gfiwaumr uloju.
Tdeq colox soze oz rto dogn pauj. Natq ow: Vei’cw caaxz faj ta nixxgaiz rhu tinjej qzuvam uz vki fafu gedu ol vcu sowj.
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.