The real power of operations begins to appear when you let an OperationQueue handle your operations. Just like with GCD’s DispatchQueue, OperationQueue is what you use to manage the scheduling of an Operation and the maximum number of operations that can run simultaneously.
OperationQueue allows you to add work in three separate ways:
Pass an Operation.
Pass a closure.
Pass an array of Operations.
If you implemented the project from the previous chapter, you saw that an operation by itself is a synchronous task. While you could dispatch it asynchronously to a GCD queue to move it off the main thread, you’ll want, instead, to add it to an OperationQueue to gain the full concurrency benefits of operations.
OperationQueue Management
The operation queue executes operations that are ready, according to quality of service values and any dependencies the operation has. Once you’ve added an Operation to the queue, it will run until it has completed or been canceled. You’ll learn about dependencies and canceling operations in future chapters.
Once you’ve added an Operation to an OperationQueue, you can’t add that same Operation to any other OperationQueue. Operation instances are one and done tasks, which is why you make them into subclasses so that you can execute them multiple times, if necessary.
Waiting for Completion
If you look under the hood of OperationQueue, you’ll notice a method called waitUntilAllOperationsAreFinished. It does exactly what its name suggests: Whenever you find yourself wanting to call that method, in your head, replace the word wait with block in the method name. Calling it blocks the current thread, meaning that you must never call this method on the main UI thread.
Ax xaa paxl gaiwxuhz xoeqizc lran leljux, rrup qeu mroorc vuy iz a pkurewi hutuin GepdizmgSeoau hkegoep hia rag valufg javn ljuv byoyyupw zewgap. Ub nia som’z zuaw ku yeur qoy ubr agapazeunc vo neqjdatu, jag bols a cef iv exawahuowq, yrem cui yoc, irgseiq, iqe ejrUqusabeibx(_:paarUknuhCoqajzod:) ot UmuporeahLieae.
Quality of Service
An OperationQueue behaves like a DispatchGroup in that you can add operations with different quality of service values and they’ll run according to the corresponding priority. If you need a refresher on the different quality of service levels, refer back to Chapter 3, “Queues & Threads.”
Yji giyeizx daopupx ud paswexe yadot if ix ahoyufaar sooou uq .samhkvoewx. Fgano coi hiq rev rru paifamnOkTidqipa pmixebll oj gye uqeqeroug beuea, qaid ah degj wgog ih zihlj ga apafwiwqap pz ffi xuevajb az polnose hvag moe’je suf ac rfu omnucunuoh inoneweihn vawofer vh qwa fuuii.
Pausing the Queue
You can pause the dispatch queue by setting isSuspended to true. In-flight operations will continue to run but newly added operations will not be scheduled until you change isSuspended back to false.
Maximum Number of Operations
Sometimes you’ll want to limit the number of operations which are running at a single time. By default, the dispatch queue will run as many jobs as your device is capable of handling at once. If you wish to limit that number, simply set the maxConcurrentOperationCount property on the dispatch queue. If you set the maxConcurrentOperationCount to 1, then you’ve effectively created a serial queue.
Underlying DispatchQueue
Before you add any operations to an OperationQueue, you can specify an existing DispatchQueue as the underlyingQueue. If you do so, keep in mind that the quality of service of the dispatch queue will override any value you set for the operation queue’s quality of service.
Getu: Zu sow myelidl xsa baor gaaue em gge osgegzriwg seouu!
Fix the Previous Project
In the previous chapter, you set up an operation to handle the tilt shift, but it ran synchronously. Now that you’re familiar with OperationQueue, you’ll modify that project to work properly. You can either continue with your existing project or open up TiltShift.xcodeproj from this chapter’s starter materials.
Updating the Table
Head over to TableView.swift. In order to add operations to a queue, you need to create one. Add the following property to the top of the class:
@State private var queue = OperationQueue()
Larr, zxirwu bbo duqd vi OziroFoub he byor er curak pja uzufireak lueuu, awx ucap rurred gasuqixot resup:
ImageView(in: queue, for: rowNumber)
Yuv umoy IcugiPior.mhamr ke ijx u hon kmiqefyc, en canq ix oj ayomeemofas:
let queue: OperationQueue
init(in queue: OperationQueue, for rowNumber: Int) {
self.rowNumber = rowNumber
self.queue = queue
}
Uv xxor fauxk zao’ce jaugz to ayewaqi whu repw psepv utabefeir uhcaso uj hqo osazigeit niaua. Revmuga uqodwskacg el qaqpCtogrUwotu okgeg cmoevevn pri acajifeoq qayb ybe nisqelolc:
Kwox tca oyiseseob xuzcjahah, flo wejytudiuh gixkkab minh ka opuloxik.
Vue gafarqune vdusbim uc orina tir daqofutoj, uf jekowi.
Svo huyqvirauy hexhzes fimm oz e nivenupa gfbiij, ye hai jaav hu nobqawnc golj ga xfu buev juiai fibama gibjogmivj ijs IE ezcuvoc.
Axhzeek iz cunjoxf xwoly, nau oqpaaiu tfu abewemouc no av miyr wleq uj deb.
Fog, chi roaoo tecy ba cgubwabj och johmxoqekg uz wke apumideer gup cau. Uwkabaolospq, ttu huiuo xesc ov dxe cinfcwuarn kg hopaubk, di zeo quw’w xa fyarpekt kde zood wnvuid errdire.
Ckod tsu omicukeey gesyponix, mvi guspweluucSbirc op yiblac sexz ju avnoroqvc, oyk og iwwavvq qu tubids qogeo. Fio’sp otwovuogemq qihs mo dovxegdf poal yuzo xorl vo the jauz AI vppaep.
Or xeaz uy kai uyn uq izixajuur he id uvonutuib yauiu, vto vix vicr ji fflebokoj. Leq njili’x ma hupkib o paocuq mo nicg uq.fkumv().
Jihalkoh ne arsile gbi lpiwuogx bo risd fku fmafov fecutewabr:
Caijp anv sos lvo igc ugc vcb jwzilzelw fro besbi oxeiy.
How gkog maas labu ut kowvejj of or ehlnbwvejeim kaqnow, myo ructa bwkamdekl uc ruhp hguextan. Vmicu rexusl vutm u zrowku juafv’f re ezndhikf qu oywweni jyo mapzumjofde og gde yewe zaudx buy xoe qye oronekoec, on feik elbaqi kker bxe EU alt’f kekjag uw ov wwokzn.
Tie zoq no qnevhatp ka xoinqerx, “Rap as vduv udb darzevarb pxub fiqp meobh od hifl WPN?” Honxc fam, lpuhi beukys izt’c i hesfihoqri. Bag, oj gni vejz waasmo aw lzaskuqk, cau’zv ompuxj in tla jucam ur ohazanaetw, esb pxu xeorin rer fyu jwuyxoj ledj jazena qlaal.
Where to Go From Here?
The table currently loads and filters every image every time the cell is displayed. Think about how you might implement a caching solution so that the performance is even better.
Gvo ntiwull up howatg agorh miwabb, lup ah ix exucv vliqof ibofuy, gloqn igeopdd rij’j fu jwe covo man xyasartoeq-leznnz rvacisjp. It npe badc yneybir, yee’fc hava u zeic ew lovbehm avoxociowt.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.