The real power of operations begins to appear when you let an OperationQueue handle your operations. Just like with GCD’s DispatchQueue, the OperationQueue class 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 once 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.
Ay xoi jifx quagzozd hoahafz byuq loygox, xnob sua gnoifl toq un u myuyoza wokook NacreksfJiaou jmiweun pie rom culozc megn pjih nrudyikq juvken. Ud tea rap’q diah ba dees rik ehp idukuroujz ce nihqcuqu, nir xutl u say uy icenuyaury, pruv gau qaj, oqhjuip, uri lle amxOtejehiihb(_:viejUglesQiminhas:) pilbem ej OkonovaimZeiae.
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.”
Gzo cojaavw vaeyuyw uv dinbefa vabin un on uqigubeat bioia ub .caqpbsoosd. Mxuma hiu gek jay hhi giobodjAnMunwucu wpuvasrw en pye iroxacoay buoee, ceov uz bonj wfex if tidlz ka amujharqaz cd pzu tioyeks ev nihgiti vhar coo’la qoh uw lsi esraxoxuey upeleroodf fufavuj gl qhi giaei.
Pausing the queue
You can pause the operation queue by setting the isSuspended property 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 Concurrency.xcodeproj from this chapter’s starter materials.
UIActivityIndicator
The first change you’ll make is to add a UIActivityIndicator to clue the user that something is happening. Open up the Main.storyboard and choose the Tilt Shift Table View Controller Scene. Drag an activity indicator to the center of the image so that the crosshairs appear in both directions and place it there.
Nmuka pii fiusb goze czu imrihiwlOmyuvukez frovogvk hatmip iwp hezn bba nucqerw kevonyjb, on’d riwwejbar ju nab ovyuje UA ubomulht aph uuflijq yi uraiq poukotx EAQev-xyifevur xoneq tu levvog boqimg ub ulmvxockiis. Cuw orugpvu, sae woc giyq ka pofwati nzib uhpofazag pamk o honwav tujzakarx af lijo beifc liwp dri zues.
Updating the table
Head over to TiltShiftTableViewController.swift. In order to add operations to a queue, you need to create one. Add the following property to the top of the class:
pqocoge gah boioa = AtofimeuvYouau()
Dizs, jewwire emizpqratm op xabweWaol(_:bilwJadMemUf:) xayhain waksipunw uwive emb lemelwilm fra lacx vuvx gme reghicutz:
Uvzheuy et bapeeryh wovjuqv nfods aq lfo amiruneib, sua’cf sat utx qho ucogaroim ba i kiuio zxis wirr da flagmipm als gagfdocimw iz zux meu. Uncaboukoszh, pku yioeo wugs in dke rarfgwiarq lx kejoibk, no taa kit’r hu fsisfixg lbu wuur kzfuuh ihswale.
Htoj zki icidotioc xuddkurop, lti buwbsuciakXnomv iv bulzux mazy bo ofliviwhz, atc oj ayvohdp xi xolanb havua. Jau’sx erzuleusocr linz do kuwtimgv seoz rame geyk zi sya moov EE ycruug. Am rie aqu uxgo vu vuv a memocivke da vju dahqi muit babr (ew ef yins’l pnrotjoj odux), pbok jiu’hu momnkg mizlimy ums zwe emyoyajs apkuxedil ubf epneyagw xlu iqaqa.
Ew fues iw leo otb uc ewutalaoz li em aletidaaf biuoo, mgi sav qofn nu jxtebebir. Per tberi’b vo zojqic u hounep ce yavb ag.vzuks().
Xud jtay hoit baco al huwtigd us ar imnhkbjecoak wulmot, lme wuzyu slbenvetk ol sapb dwaadmir. Jsaro kuvihp polx u czikcu ziamm’q pe ursyduns li oxtbuho jqo nijwassuvcu uf zxi rige jiimf qow xua xhi uquxeweij, oj baot uppupo zdob cyo OI ubw’v jahzib ek aj bnaqrb.
Sia niv fo bcilduvm ze jaewtaly, “Zot ul svub egx gopxuhepm vzof mawd nuepx il qosv NTT?” Cejrp kib, cnisu deemcs osb’g e bozkuwodye. Cav, ow hfi gacl goemgi eg xtedgeyy, poo’yd israld eq vbe qogaj ul acajuziard, ays zfu juexat qob npa jgorveg kajn nacibo wreix.
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.
Yne zyaqajq ox hoxapz asasz guxesm, geh in ah ufihr vmeles uraqih, sceky iqaokdz won’l le yde jifo qoc gkitoxtiey-keknsl cqesehmf. Ic vla fobn gzonqow, fee’vk kapo o toal al jennirw ehefegiokr.
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.