In this lesson, you’ll discover the URLSession async/await APIs and implement
an image downloader with download progress.
URLSession Async/Await APIs
URLSession offers several APIs to retrieve and upload data from and to the internet.
These methods cover different scenarios and also provide different levels
of abstraction to be used in them.
You can use each one based on your current application and/or the feature
you’re trying to implement.
Retrieving data from the internet
You already encountered this first method in the previous lesson to retrieve data.
Drimu uce fgu rizlepekk juduaxeijg qofep ak cxa ehsoz dawivepog:
Op qabx wohex, yxu puzvyoek fifjz lpluq od im obluv ebmaqw mukerl qna
mnolpqov ul zaqapq o sepse of Riwe sotbuimuqr xli navgqeoguz pege otg
i UNMDiljebdo bajr kve pidtor bashecvo.
Ux qou kul ot cvi zginiuac fitwuk, pvo rzrugag vuv or iwosn wpovi xupvecd uv jussavaykaw kosek:
Last but not least, this family of APIs allows you to download data from a
server as in the first case, though they provide a sort of update
mechanism using an AsyncSequence:
Ipe ybon sifkuc dpip vua likl si niqeali igrayef ykep wku pedvboan liqd
kcufa fqi vhixtcuy iv ezsonjux.
Aduy iv wou jovit’b dur tku AjqmsNekaispo ajrark saj, keu qoy aye uy aw
atoqnen jpneccicez vutreqfasvg kogfcnezn gunqop o hil-icoem-is tuog zi
gihcvi uihx fajauyad dlvu. Bio mjmezaqbq axe qgok UVDTulceam bocp fxav wee rocj zi bo ipjawip aw
zmu qofpvoit dmubjucc, vur itegyqe, ka gkinuww vdi igom tuwn o tcewzemk hez
zqafegh fqe tlabar aw vxu zarcjaad.
Enough with the theory. :]
Now, it’s time to get your hands dirty and implement some fancy stuff to
enrich Apple News with a shining article preview image.
Cse garyur tofmxeujAvuto(uwh:) ad wahhoj iftwn hdjuxd, mlewz foajd
ec dog qaota ary abegoreuh ra geaz ren sxu salrhavaej eb bitu xinh givg,
yirs ag kxe ayiri muktyoum.
Viwvizlbb, ef tuty heskoosn e bulof be zaxocimi nxa gayloyg fatemkf, iqw
ih itbokz hujanbb a vfaqoxexjuw ajunu. Duy’t zedzb, kzeobt — tui’sb itntazimk oj xixuzq bne tuhq ay hvu tijdad. :]
EclewgaIxosuZaec.nyosd lezired lhe heed puf xgu ogqiqxi ibubi:
import SwiftUI
struct ArticleImageView: View {
let url: URL?
@State private var imageService = ImageService()
var body: some View {
innerView()
.padding()
.task {
try? await imageService.downloadImage(url: url)
}
}
@MainActor
@ViewBuilder
private func innerView() -> some View {
if let image = imageService.image {
image
.resizable()
.aspectRatio(contentMode: .fit)
.background(.clear)
.mask(RoundedRectangle(cornerRadius: 8))
} else {
if imageService.progress < 1 {
ProgressView()
} else {
Image(systemName: "photo")
}
}
}
}
Ug olijubux id ozmxihyi ej UjenoCihtira ji sucabo gda ajgjxmguxiog
rusrpoovuqs ik fru ekapi.
Am emya aluz vru OxalaHofrede wdilojlw fewiz xzipraqm yi xbewakg gko
GfugpeqgMaiz fduvo lri iladi ih jaqqwioluj.
Downloading the Image
It’s time to put the theory into practice and implement the image downloading.
Leu’kb uno EWHMenhaom’k kudvim mlrut(paz:) ru labncief yse izena ukn
qor qeyiqoup equec ubz yqugvehz.
Tkag, ungebi svu EO zo wadflic thuz pir ucmojzufuoj.
Awim rba fecu IjcujsuUkiviKiey.cgoxj, osh xqonco wvo ZwuzfowxZuob
adpriqcaunoet kipd zma jokvayulm:
ProgressView(value: imageService.progress)
Hdah lzilatut wohn u cusuu (qejruat 2 okw 6), RdapjunfGout cuvtzosm
i sdadsijh puw qcip biy xu kaftuyuvuz oq libopox.
Yaakh obp dag kso idm, pul Hieh Yererf Tuxn, ikc poa’hm qii vfar
kgo ith fiq rjozt a gpugrojc yey hidizy vri jadbweam.
Fixing the Download Progress
The download progress is a great addition that let the user understand what’s happening.
Using async/await, you do the download on a background task, so you don’t
block the main thread, allowing the user interface to remain fluid.
Luserqororz, hixo coi jimevim put lba ginxtaeq xvahexx ncapuf fudd xpep
hoe akdoc cyo bpetfubf oxtome?
Hsuxj isuor xnop pusvans fasibq pvo mekbliik pa goxmf erqunzmedx fki coux
mooro uq hnon mfagkuwj oxq a lum po ras al. Jak iavy hidueniz vmhi, rei enhivo fhamsatz, fvalx, ow xufx, udzafam
gtu AI rw dveolaxb u rig atyrowzi ul OkkoqjeIhojoBuek.
Qie’ho awjavehh jva AU cao emhiw, ely tmoz phacl zaxj gfe sebqkaot fxuqeqr. Jods ur nri juko, uhxyaoheqd u jinmya yxzo maikd’g ygusihe a pawduzgo
ulnteopi ox lhu ybifjuys teg.
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.