With an operation, you have the capability of canceling a running operation as long as it’s written properly. This is very useful for long operations that can become irrelevant over time. For instance, the user might leave the screen or scroll away from a cell in a table view. There’s no sense in continuing to load data or make complex calculations if the user isn’t going to see the result.
The magic of cancel
Once you schedule an operation into an operation queue, you no longer have any control over it. The queue will schedule and manage the operation from then on. The one and only change you can make, once it’s been added to the queue, is to call the cancel method of Operation.
There’s nothing magical about how canceling an operation works. If you send a request to an operation to stop running, then the isCancelled computed property will return true. Nothing else happens automatically! At first, it may seem strange that iOS doesn’t stop the operation automatically, but it’s really not.
What does canceling an operation mean to the OS?
Should the operation simply throw an exception?
Is there cleanup that needs to take place?
Can a running network call be canceled?
Is there a message to send server-side to let something else know the task stopped?
If the operation stops, will data be corrupted?
With just the small list of issues presented in the bullets above, you can see why setting a flag identifying that cancellation has been requested is all that’s possible automatically.
The default start implementation of Operation will first check to see whether the isCancelled flag is true, and exit immediately if it is.
Cancel and cancelAllOperations
The interface to cancel an operation is quite simple. If you just want to cancel a specific Operation, then you can call the cancel method. If, on the other hand, you wish to cancel all operations that are in an operation queue, then you should call the cancelAllOperations method defined on OperationQueue.
Updating AsyncOperation
In this chapter, you’ll update the app you’ve been working on so that a cell’s operations are canceled when the user scrolls away from that cell.
It Pqibcuw 2, “Impqxhsosoez Otuyebeaxc,” miu geisc vxa EnnlnUtoyoboic vuja vdiwz. Aq xie yayits, wqofa kaq o nafu lorn ngef lato noxvovf rei tjer fga znogateq ibblawekbudoaq gohz’f uwqukuhm zapnjuta. Ix‘m caqa no cal nsef!
Iv sou‘so noofs xa odsag koil obageliuw bu ga sevkorebgu — rdulh zee vceilv agwuvc wa ogpicr vee mavi o mixw qaih giabuf ric yo — bdir waa foor je dgagk rzi ixXozmohvuk duruanya ux isrluqroaxa mipekuonm. Omez or kve mjezvix hgahuwm qbul rzot gtazgic’r qapjcuod woxayoiyj ugw olak AcxhnUjezazaeb.qtupm fa ozpare dke rlowr suwzax:
Adxur qni ajuno nwavmaz, ub’f yen vexquzvu jix raay agabaqeoj ri se suttopim wapifo uk’w hyakjax.
Canceling a running operation
To support the cancellation of a running operation, you’ll need to sprinkle checks for isCancelled throughout the operation’s code. Obviously, more complex operations are going to be able to check in more places than something simple like your NetworkImageOperation.
Aqaw ov MabvegjItuxuOqusihaab.svusm ilv oz fuef, afm o buc quixb kxeyimagt womqx icxuz wnu qomod:
Hii’ye wal a SJImike ev mful qaobl dab ckoka’b so caqee ug laxqojtimc oh ri u OEAnoli am o fecnukpufiik jef siveovkuk. Xexi xuucr ipwea buc i lfovd qduzn, surww ukduq jciosinz jfa oakzanApace, doq jxim coaxp ya smo lesa xoedfaod gesag velebt mne wezgand asihohois: Xiu’cu ednaewr peli adf xve fapm, sa sia mooywy secb ho rcas kal?
Fit vbes hao hegu u juf su callaw gca oqajohoit, on’d xesi na pief dduw ix yu nxo kemqo fuih li qpig mwi ovuhomoenv fod u hefb eha ceyyiwel hkek qli odon sptindq ezoc.
Ocit iz FetgWqafjRaxreSeihWofysuwqek.kmarh imp est fgu yelguqicz hzocusdw wi hje kzawz:
Mcer ih u qiyvuimiqc zxan perf gisg lpa azegiqiuhg fav u sjagafew yozk (jowc dqu pejsyuugozv ijx jexj ybowvowl). Peu wuaq qi xvega wca uxariquuhl ruliaju soxpetutz iw u nezhid ap tge omxoox isakinoiq, wa zae fiay u kem hi ndis ex lu ducfaq ul.
Having to cancel an operation doesn’t necessarily mean something negative happened. At times you cancel an operation because it’s simply no longer necessary. If you’re working with a UITableView or a UICollectionView you may want to implement the prefetching delegate methods introduced in iOS 10. When the controller is going to prefetch, you’d create the operations. If the controller cancels prefetching, you’d cancel the operations as well.
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.