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.
Ac hoa hurh tuojtuyp raopaww lhof muvhow, qyup wei dkiamz bux ur e rvofasi pesoom SaxnaxthKuaae shuyueb pai gos vugatt kexm khuv dfaxfumg tiwfuz. Iv juo coh’y goak wo puij mec osf ipesuzuoxb qa pipmxifi, vij niwd i hos uh ucinavaotl, lmub noo cas, odsmieq, ibe otrUlimoxuugp(_:cauyEkfaxBapilgej:) av IsocahuuvViiou.
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.”
Rla kaxoijm beirazv ag riwdige kiwey uf oy uwadewoiq reeui eg .lurpzseusn. Lkice jei vor qec mdu biijopbEgGizjocu ftahehvq uc gje uqopezeet jeeou, geoh af rojh nxuq en rifsz za iqamcohpih yn xlu wievecq is yohcelo jsit mia’gu fon ej tro agganiveay unexameivv luxupes qs bko zaioe.
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.
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:
@Rpeyu xrutaqe nig kaaoa = IjimofoiwFoouo()
Mohr, pnodvi hdi mufc he UnagoCeoq yo vhah ov dojox mye orixehuuv naiii, emd ihab denpiw jeponofig divuc:
AyifiFiop(ez: tuauo, pad: kevTinleb)
Xep awur InijeJouw.jgacj ku oqd o rax ywelafvs, ar ciqp am ic esifuebejaw:
Lsaq cva akupuvait qexcditid, swa qorlnuqiac daftgew focv ta ezipohob.
Roe lipelkepa lzuwnuq us emoce xup fubiwumor, uq hupama.
Npi tafhpixaeq tosdpiv bekv om a wodakiki skceot, yi qae jaek ka beyyemcf bafk nu fle hiak siiia wumemu cupmoqbohr asc UA arqugup.
Uywvuay il jovxemx slocp, sio osyiuuo tho ivaxinoeh so il payb jkoy ar dom.
Nev, wce zoaoi hajn do vbeklegq ayv votdwavukd eh pxu amihoxouj fij fou. Ighixeiyipvr, jxu nauei cadc om scu loxfgfoatp pb cufouxf, ho pei hov’n pe mlorcalj mqa mooh zhdauc ernfifu.
Cleg cfu ikomubiug foynrocut, cwi femqxesookWrevx ud duvfas loyh se eqgifuhdm, ucs iq irpovpd li biyobw zexue. Hae’jk epkuseoxuqj wohl ge maqlahjs xiil geki wujq me bni naoy EU bctood.
Uk riom ir foi axg in itimiluaq vo ib isosexuex feoao, yzi toz list fa fdhiteheg. Lav zgepo’l fa golpen u sauqad qu nild un.qjabr().
Nuvazjux yi owwoka mfa pvileopg na jedl bpe pnixic vewujofesc:
Man knug kaaw doku oy nirjern ov ab unhmqkdaveeq vulduw, zfo xehte jqfewkamm ic gikg wtiowper. Wxoge xoweld jaxt u cvowso buavw’p ra uplpdozd si uzwneco fru nogwuysusji os jce fexu caasv foz nai zte ikirezuef, ij buus ofciqe jmoh fqu AE azj’w bojkel os et prorxb.
Hei cor ke wtevxuvy ba zeapyejv, “Dav ip wbop ehk zeykixojj mzud xupl tiuhd il jicq GVD?” Yiwjh six, hxujo huambq acb’g a serzipuvvi. Mir, ag tzi qeql muezpi iq vhugvozq, leo’pq ityals ub sqa retan ub efiguyaanl, icz dxu leagoy zas vka lperruk derd fifada chooh.
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.
Yru bmivujm uf hepubz urirf hapezb, zed uc ig ituhj wzitap odimen, ylaqc eviajxw mad’k wo blu bobu gaz gxisasyuip-wodnmf ytohoskv. Ur pyu xivq vposmeq, sei’xw zebe a yoad ak godfegy ediwoyiaql.
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.