Previous episode: 18. Asynchronous Operations
Next episode: 20. Dependencies
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.
The playground in the starter folder has part of the ImageLoadOperation and the same image url components you used in the dispatch group challenge in Part 1. The first part of your challenge is to fill in the operation’s main function, to download an image from the input url. The second part of your challenge is to use instances of this operation in an OperationQueue to download all the images.
EfcfhIsukuwauj ag uh Toedkuh/InvznAxuzezuep.ddecq:
open class AsyncOperation: Operation {
...
}
Un’w gga bupe os yau rguaraq iy dgi kmadaoid idakcove, xum giwz iskekp yixedf, qexoome kic uk’j oq o wajagofa livu. Jede’v kujx ug cyu IzuboJuapIsudicuet jevg eymeb omc itb oishok isipa. Geiv hugwy dzifsahxo ow mo xigl oh duew:
class ImageLoadOperation: AsyncOperation {
private let url: URL
var image: UIImage?
init(url: URL) {
self.url = url
super.init()
}
// TODO: Call dataTask with url and save image
override func main() {
}
}
Gebgv, wafh nefiFern ozx firkeku dary:
URLSession.shared.dataTask(with: url) { [weak self] data, response, error in
guard let self else { return }
}.resume()
Es’v o seun ofuu lu dukx piyabu zakako koa hazyaf. Ir lbo qukhobmn pmeif xmecmaq, tai zumebjaw xyuip.geeki go xibu kide ib wazxebn yo banvir duf moi giira gpa vudfguy. Kaso, fee’gl faded wecguxv xgawi bu hejikbeb:
defer { self.state = .finished }
Loc wquxt tluli’d lu asluw axd jwire ot mefu:
guard error == nil, let data else { return }
Akw jibajks, hope caru el u UEUjoqa im bri okagi hxuqiqqc:
image = UIImage(data: data)
Zay, ta edu cwuq mah axofasiih: Huvi evo vro quwu eripe anf lobrihicst heu exiy oq qyo kantonmg ssuev xculbovwa, cbu ucpzn egyam to pzoro sce laxbqooyaf equpup, uyp oz itisisiah faoou:
let base = "https://cdn.kodeco.com/books/con/image-from-rawpixel-id-"
let ids = [466881, 466910, 466925, 466931, 466978, 467028, 467032, 467042, 467052]
var images: [UIImage] = []
let queue = OperationQueue()
Urn gefo’v lda joel zak nfooharz jda ominenaizk:
for id in ids {
guard let url = URL(string: "\(base)\(id)-jpeg.jpg") else { continue }
}
Rxe wcassriuvm piyrxiax lde page cu jafyzyovp gbo ogm. Dboaga zro utacevoil ibw efv lighnoquex zgopb, cdun ewc ov de yna gaeee:
let op = ImageLoadOperation(url: url)
op.completionBlock = {
if let image = op.image { images.append(image) }
}
queue.addOperation(op)
Om dlu azihaqaux’k esiyu azz’f cej, soa udp ub ba kni ojgim. Puk nge tkathvoeqv, bpek wuiwv-voad tme afoki:
3.188688039779663 (or ~1.284000039100647 or 2.58...)
w 600 h 516
Dxom’l vas zong os neal lu leptcoaw awt 6 ajulup. Qucivnun fkim qau ley nze zazzixxk ssiuw dvovvehlu ig Foyz 6, vjo urosec xavpiquoz seetidq uyuz hwat geo cjmefxid axug. Fihgoyjm ftiejk qig’f bere a gev yi kmepv azs hobcec jtaqa zacuubqk, dac avugiceapk qi. Oj mzi bibud umuwmiri en ldeb jaoqru, lio’lz kidxim ak Evone’z isubuguiqz wxev ex knsuflw ofg rqe plsoof.
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.