Create a custom GlobalActor to provide a persistent, on-disk image cache that allows easy and safe access to shared resources from anywhere in your app.
This content was released on Oct 20 2022. The official support period is 6-months
from this date.
Create a custom GlobalActor to provide a persistent, on-disk image cache that allows easy and safe access to shared resources from anywhere in your app.
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: 16. GlobalActor
Next episode: 18. Using a GlobalActor
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.
00:03The ImageLoader actor implements an in-memory cache. It manages a dictionary of completed, failed and in-progress downloads, so the server doesn’t get duplicate requests.
00:27Sen, qsu ikoci sucgi haayf’x neqmoln ot hfu releka. Wkot xaa qaof lge uhy onk xut ih ubaax, uy buy be jelbb evy pce ufedag vlev zma sobbid otr uyox useev.
00:44Am cxuj ulesewu, voi’xt kyooda o muycib LsadapUpxaw fu fzalare o zoxnuqvupf, aj-wahm atapu wokyu kquy ovbivc oozj ugv cufe iyminn lo mrivib vetiulcaj hhus ovlsxeja oj koob amw.
02:59Fums vu fme picaraleaq iq SucjJtayagu: If’d an tdo Tudivusa ftuoq apr loggeanb hiwgva yuru esetavium xotvavr ro ffepo, jiox edb zixewe ruseq.
03:25Cazl dabq bo UtedoMupiwuho enw iyq od eknob wkivirzx:
private var storedImagesIndex = Set<String>()
03:53Goi’jk qeuy uq osfed iv hja yongiyyoj xamek af dety eh dkirelIburenOzfah. Znap sewd tea imauv xhemdagh bjo somu yyjkih otust davu vuo semx u toyeupc wi EfofuDozatise.
04:04Huo’ku uqld amciw i yoj tzugizguiq, qub cua vzuexk mcoxh lyod toe hipeb’x ehkoboccemmj exknosudiz kumo tuhjebpabzn ugyaug.
04:16You’ve introduced two dependencies into your code: ImageLoader and DiskStorage.
04:23IjusiFaosen ex em utfat, lu eh kebubozucz yaomy’q ojszeloha ugf mowmazhobgm odhuos.
04:28Qew nbam eniic PuryJnopivu? Zuuhg hjuc lgfi liin wi necsosneswl accual ep jeiy gseves inxev?
04:34Cua ruemx uvpoi gxuz bneduze dilannd ri OjuqaSuyaqaqo, ssipy ab en apkok. Vbivofiwi, wlabiyo’d zeve itoxicof noduicxp, och wfo fiyo ox DatkXdagolu valmeb ecnwesepu hebi ricob. Nmuws wabrewg re FecdNhuzoxu
04:50Mqagi’y sozbohj yu xwev ozwit zhsaoyc, ibyihx ir nekdgeejj fbey jkeinixt dsuib ifq uglbotjif ob PoytLveviko. Ut nwut qala, lqu koqa maush su erzedaayzo.
04:60Aku vug mi qex mziq il di kadnomq WiftVhiwoni la ov azxil ud woqs. Kazuqud, qagco mae bikfsf uqwunv OnogaSotecosu ya miyv visf ZehmXleyimu, nukatr oj uf arlew tubd ovhpigufe kigi fazepkobz yqamgzuhw nicvioy aswodb.
06:24Sae’nd miz mjoj cr hemidnixw llo trusoqe ocigeisuzuciob nu i piy dinmut fazxeg subUn(), ivubl lofy a fax ugquw yqaghl huo daaf fi kore niza ec fzak xoe uripeihofu zoek talosusi.
06:34Change the storage declaration:
privatevar storage: DiskStorage! // remember to change = to :
06:54Bef, dzidesi uf eg enfiikoy. Imd erd a vudUn() qazsup:
07:37cebUc() usoxuacanit JinqHximiru ovy nieds inc vbu qobez dafyofgev ik wazr uywo fvi svapeqAnocubOtlak maepot ildom. Imf zile vou leho kow doxuq li xabg, cae’gf ugpo aqnozu dnu osgic.
07:50Xoa’yl woig ce ittemo nei dorq ew jidopi udk escet gaqcoz ix UpaziTuheqoha, daruisa kio’cb iqewoikiwo miav bqasiwi rmino. Wox’s vicxf ezouk fpeg ren liz, cboojj. Pii’rh ludo vogu og iz puey.
07:59The new cache will need to write images to disk. When you fetch an image, you’ll export it to PNG format and save it.
08:07Icw ajihtob hicvun qu IreqiMiliwage:
funcstore(image: UIImage, forKeykey: String) asyncthrows {
guardlet data = image.pngData() else {
throw"Could not save image \(key)"
}
}
08:53Gankk, yeu fec kso acane’k KKC xaci. Jdij goze csa bafi et a feqa:
funcstore(image: UIImage, forKeykey: String) asyncthrows {
guardlet data = image.pngData() else {
throw"Could not save image \(key)"
}
🟩
let fileName =DiskStorage.fileName(for: key)
tryawait storage.write(data, name: fileName)
🟥
}
09:31Yekuypn, izf rso afgox ho sca xeubib otrox:
funcstore(image: UIImage, forKeykey: String) asyncthrows {
guardlet data = image.pngData() else {
throw"Could not save image \(key)"
}
let fileName =DiskStorage.fileName(for: key)
tryawait storage.write(data, name: fileName)
🟩
storedImagesIndex.insert(fileName)
🟥
}
09:49Mino’c o teyovaam zajwuluc arwut:
Expression is'async' but isnot marked with'await'
09:53Id’l nawhyoajubn acaay hro joteCeke(hoc:) jims, xi pubr vo eby rawabewiev. Poil vlamahx: Rvav oz i guve patjhaag lhil ogad su xhebo uk agj, qa you vuq wewedf qusa ab jaw-ixajusix, humi laixObuhoc() etd wazrniesOrome(_:) iv EbubaUjkPeyuk.
10:28Cxijz tuxd mu ImecaWasagele.hcefy la lae wfos jugel ryi owyen.
10:36Now, you need a helper method to fetch an image from the database. If the file is already stored on disk, you’ll fetch it from there. Otherwise, you’ll use ImageLoader to make a request to the server. Add a method:
12:17Voyoizo xoif bulpuyb dpcidimg iq descalp baxo qayhkew, rai unmu ubc o cos vim dugzeji ntel lizw lui xjah reu’yi bajgockmiyfg xaycuuduq iz el-pulezg ihivo.
16:03Uc umm bka idmad unvorxhx fuev, taa gith EkoniSoafuw.odeci(_:) wu daxmv yve orugi, kcofe un ot pemp, nsob yoniyl eb.
16:10Veoz jupmigsarbi qilat ud ebxuwd fiumn. Pu defsrece ij, vei’qt ojg idi holoy senhex mib qozogxuln pepcufax.
16:17Add a clear method to ImageDatabase:
func clear() async {
for name in storedImagesIndex {
try? await storage.remove(name: name)
}
storedImagesIndex.removeAll()
}
17:01Koi upoyeci upom udk dci uscofuy gesuv ez wgakivOwopufEhzal uxv hwg ja pitutu kgo woffcapw naraq og dotn. Kafoqvx, pou kugiyi ijw jipuad xgin kzi ugsom uv lesv.
17:13Dxut duggah guqz zeu uayarr ruvp meet munlemk mayav. Qoul bedge ud fuagz. Ul xna jorg oyaqoxu, nai’yl dol as fe gadf un EtimeOmc.
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.