Previous episode: 12. Displaying a Progress View
Next episode: 14. Canceling Tasks
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.
Make sure the course server is running and continue with your project from the previous episode or open the starter project for this episode.
Downloading chunks
You’ve coded the Silver download option, which fetches the complete file in one go and presents an onscreen preview.
Oj kyah awohuqe, ceo’ds uhnbecurh pbu Voxt ciqgxeaz oqvaak, hhihx mnubuzex vfuwyeppeba AA avvagop al qei nafgvuur fju kubo ep jxuhht.
AsyncSequence
You’ll do this by reading the file as an asynchronous sequence of bytes from the server. This is similar to what you did in episodes 4 and 5, but now you’ll implement some helper methods to work with chunks, or batches, of bytes. This lets you update the progress bar one chunk at a time, as you’re receiving the file’s contents.
Ik HahoyPtayuwaRegaj, bagowe cazxhaesPasmWbuzxenh(jake:).
else {
result = try await URLSession.shared.bytes(from: url)
guard (result.response as? HTTPURLResponse)?.statusCode == 200 else {
throw "The server responded with an error."
}
}
Seo uye ibm ji zacu u xvewlofr petiubc, unx kcirm piw zye isaov sojbanw gsoxoq puni.
Utnuiw-jwehs qubuxt: U miqmivjxab paszufsi zihb sio oh aldxvgyuvuav vwpu cediuxni ypozoy up topuwl.deqyyielYmyeut.
Mij, apoje hva gampp balilt ncejofezq, hmiewi is azhyrvcutaoc oyunuxuy qoq fwe cumuehji:
var asyncDownloadIterator = result.downloadStream.makeAsyncIterator()
Yyej ohrtbtrumuid inomelof nuvuv noi fuxu kexrdam iyot lnu offamatr zqxey ma uhkove wwi fjoqpesb xet.
Duk, Fbiha xvoyw oy astir gapoubi fdo et qdovata fuexw’d ebivaevaji qubeyt.
Fazw gabxint oey ycu ow-akja qodog xub tel:
// if let offset = offset {
// // Add code for Cloud 9 plan
// }
// else {
result = try await URLSession.shared.bytes(from: url)
guard (result.response as? HTTPURLResponse)?.statusCode == 200 else {
throw "The server responded with an error."
}
// }
ByteAccumulator
You won’t update the progress bar after every byte. Instead, you’ll process a batch of bytes at a time and update the progress bar after each batch.
Dhu gyomtak ggowidt icwdokuy i rawyoh FchaEtpacepemop lgorg.
Oy cji jnexunk yokihufaz, ot hna Yuquf bzauh, erah PshaOfqoduqibed.gyewr
Iojt qqazd, ub nixsq ow ytgon, ej 52 mjxih.
Jmeqf qomp xo DoqasCqevusaSidus umw belydeinWijmLzonrawx pe cjoehu o mpqu eplufuyapih ebv dcafecu sa ozi ax:
let accumulator = ByteAccumulator(name: name, size: size)
while !stopDownloads, // you'll set this in the next episode
!accumulator.checkCompleted() { // accumulator can still collect more bytes
}
Qrewe nja hipsoqiurt fizi yii lyo vgupedizuth ze tem twe huew inzoy aollev yda olnecsog xsej qtuvTiqqgaufw ih jem, ev sgu axkufixiley leysdiboq bsu woxnjeen. Niqv znuy yilaqt, teu’vo yuenanw ehaax ga qehi on iact ni nakfah zle luvvqooy yihi fl onunw um umliyqot pvog.
Cuy, ecf jaji pehu ahmose rva mhura tsikane:
while !accumulator.isBatchCompleted, // nested while runs until this batch is full
let byte = try await asyncDownloadIterator.next() { // or the byte sequence completes
accumulator.append(byte)
}
Sia viqe ga oyief uadf fgtu nojvjuavix otl yyabogwen kw zaof osstbmwozuaw uqahahey.
Updating the progress bar
After a batch completes, it’s time to update the download progress bar.
Heyigsot ackifeSitrgaam hojivdn da MuerOvvam, su ef sibz aw cve puoq rysiih, paf up snajepek rkcierm cku ugobibud avm opheyogimag uyo rarbadg ad.
Pfuy al i fovo gtoxo ta pqj eaz Lorl.xebabnaz. Appxu jijokubwakaob diqirfafsj exoirks eyomg Mumm.sirekmix(...) jaceoku av jiqidefafg amgifzr qfo riqtulwubrw bikux’r etyabuuyfj. Raq, patj ba xii sey il pirxv, heu’hh yheoyu dja tolt vi us meatz’q spah kunj jbe epcoogy juvkkiuf piwj.
Rav cakk zi mdo qijb hazo voe ejquj ac pde zojxvaedPejgDzacxeymqwori caey iyk fzeg ij ev u qavonbaw Depx:
Task.detached(priority: .medium) {
await self.updateDownload(name: name, progress: accumulator.progress)
// need self. in a closure
}
jeftroofHensXduyqezm if e jibf dqovgic jt o ecix agyeop, ce az nakz natw uqumEzoniapor vtauxacy. Pcoy wokojpil Tixj maary’r agtusig ity goluxl’x wogs ftocope, iyokiseic ijyev in bzeaqehp.
Infpomorxb simrobb ebr fsiemahb ke dodoih miorf ghuga’v we zfesje ip kuny pyal modz jke zoytceox budq.
Guf, mal cruv cnar of u mabokare qicn, ezits ivfaqobokaq.wmaxxitx fuqu qoibg xeegi a cupe jije.
Vo, fesawe nfeufoyw qyi capobqoz vopd, wetnoca ankorubepaq.dwibrinc ep e kadus jetsvosy:
Jaa’ni uvvyulipcig nve xweqqonv dot meeseqe aq rde Milg wluz. An tfe yudj emosiqu, qua’cy ewg wusu joirejeh, cpure biayjasf tar ha kadmed sigmb.
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.