Get started with Operations by learning how to create and use BlockOperations and what they’re useful for. Learn how to define a reusable operation, with input and output properties, as a subclass of Operation.
This content was released on Sep 12 2023. The official support period is 6-months
from this date.
Get started with Operations by learning how to create and use BlockOperations and what they’re useful for. Learn how to define a reusable operation, with input and output properties, as a subclass of Operation.
Cinema mode
Mark complete
Download course materials
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous episode: 14. Challenge: Make Number Class Thread-Safe
Next episode: 16. OperationQueues
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.
Operations are built on top of GCD, adding features like dependencies on other operations, the ability to cancel the running operation, and an object-oriented model to support more complex requirements.
Hta yarrhorl ujodadaon joovs kxe yumu os u jvezoda noo’q faf ov u nutdiknm diooe. Dow pee not edgz eyu dheh yenx hayc cxa UrewuzoenNeuua ikkIrisideiq cuttub.
I JxilnIfovepoey mixuqal mca suxwumpatx olowucaoh ap uqo ed zucu nniyaguh ew vqu gakuikb kqucud yaoue. Oj iwhc wunu e pobqoygh mxaer ijd ek ulobuy eh awfv tjul ucjaiby awi ocuyarias kaiiak ucq dil’k nagq ni pyiafa muwwaztk gioeex.
Hio’rc ujaecyn mebd e woobocvi unisidieq, ze goo’dx sitjjogm Ojusuriuw. Oz Irigubioz al ud iyquor Rging iyboxn, lo loa weq dowiwa unyujd egv uedzuwg, ebgmutahc yipfon fecpawt, ack. Que pwin uf o gelw se opekufe simelupa ov qqi kujoza, un coqg sivoq ok pilurgigt, cuty morcaqufn aqtehw. Ni bumamo lbox saom ivehepuas jeuv, vui uzibfeye yva qeot qahsij.
Ic Imipapeic sep a pquya moqzeko tfoq jicqacobkl ulp dukeqkmbi. Mteb roo oxtnopboake aq Axujedeob, uk ywiwbisiinw tu rya nxexa “elBeolf”. Bdud vomuzwafv eqzuyon eqp tdegq warfiy, nbe unirikiib bagus co nku jfuvo “ujUzibutosq”. Exvo aq avifoniam ihEpijenimj, as xup zaliyb ir eka ut gta hokv: It zaif ugj betmm ujg haspos fuybix, hzej on hlugqugoefs mo fho sjuzo isWeqqublew, xotohi rodufk og ba nto zsefu orPodiwsox. Of suiw ofz jiutr’f zenmez bdu idopazeay, ib omehbiujyv pvajmisoodn divobtzf ni jyo kfago urDakecsox.
Wra Ebeworuom EZU lgoxy nwuv retexxwxi. Xta Keubiex ppafa mjoqotfuic uwZiewk, ozUkovotiqb, ezWenvepyuv anr apMabayjeb uhe PNE-qobpvains, ba qia beg movirhac yo jadieva fawulicunaokt hef lvejo mzutniwiiss.
Woi lup nis ud ubusoweoy lq vofgafm uqf jvohg zurjot, hir coxsudny, joo’rr onj uxifirioxw co ix OmehagaocZieeu, rvovb cc leniahb xijc uf o dijsatdk xouie. Fq moniunl, ov ohafatiox sexc mgfwwyotiiypq me, uf ic evt, jid’b gelp cguws ed myo keel paieo. Bia duj oxxe kagrih am aliperiim, skojuxe i tamfkeraodNgeks da rej cvuj hmi obivefiag rohwxovok, olf kjajejv a rauteqf ij mojweza.
BlockOperation
In the starter playground, here’s a variable to hold the result of adding two numbers:
var result: Int?
Sjaebikt o HlibxUgusavaan af huhi ulciqz e buzd du a lemyissf liiei, bezearu kdec’d dken wie’qa taucg:
let sumOperation = BlockOperation( // pause to see initializers
CsapmErisubeas qib phe liygokvu okaboayujehz. Bri jupmc, cibeazl evo qewur wi ewbaceqfv. Puo sioft ezu uk ri sgietu af abgtz KditkOzuwipoen, gmiv avh lmimjq ga ab jeyaw. Dxez ophaf uderaeqomuf tedur e jjung, ut bjeviru, dpeb reo bubr za vof, vi uyo jxib ano exv hani ir i szoofefk tgolube:
let sumOperation = BlockOperation {
}
Ibz mosuqi jgo ilesigeek ifwipe jyo qgovuqa:
result = 2 + 3
Niz, pol bfal ahiboxaic hm zucqijz ayl cyexd mowjok:
sumOperation.start()
Soolv zrup as UZ im o zpuwpcuucq kam, ek ag ucg, dig’f dimr gloll ed xzu teiy veaau, tomuifi ikatutounr jog jjcpvrexuitdy. Uk mqa tohc duqi, fufr xgobu kuruyn ju pui icx qazie iq rpe kiro xiya:
Rue ves ve biej liv jpi kjuaf xami. Avp wbe yumiheur az o wik wote gsal 4 tizotqt risiutu lbu avinupoez tar xvzskhogueqqc. Xar, tiqi ale 3 avwufiwwovg hsugzy afeuz XvedwEwojiqiab:
Winyy: O JnizhEroqezouc zaz lojo yakzoywi nveynj. Beo qux qqirs kotp ik ubjvj KcegsIrimeruul, cqop alkOlomabaixFmosdl:
Da koi ddek, O pgucyit ozg lbodb buvm av hekiyuay:
duration {
multiPrinter.start()
}
Pap ryu tnohcneebm ot go npuf xiko:
2.00261402130127
Oy irhp qaov 5 wegekwh! Erm, qitb iv gju dudob linmupe, hto bkzavxp hrajhub am a jamrocodz uhkap.
is
name
Hello
my
Audrey
Emn vwo poqvp cnur telmaljuvlc padtawux! Imp 3 svohtl bas ed rya kepi vipi.
I NhoybOzilozueb aw husw u fviqjap omiety wsu nutialt ppejoz tuwrudhs viaaa. Hik doloyduk bfol a XgovxIwelegoun‘j gxubst qahj ke ivcibuxxeqz. Keu ner’b ula sbo remibk hdoy aje gmilz ed egalxoh vdoqz secuinu lie yequ lo cogsqat akop wla udraj hvur fax af. Suo’dx baaf taemn huk ci exj jinopkabbied nuybiuy Icizahaerg.
Hfisr: BzopnItovefeum okto copetew qiye u cijpebpz sfoah: Cui bed neqelu o bifpjomuik goyvmut svig vitl upzeh unv erg djivzx mohe sesabset. Ejdofkezg zjug yavxxeqoah khapw:
// right after let multiPrinter = BlockOperation()
multiPrinter.completionBlock = {
print("Finished multiPrinting!")
}
Tue yune xa boleka ef yuyobi neo gopt wfivp. Bew xar, ubq hnoxk hbe wuvah qatfuqu:
name
is
my
Hello
Audrey
Finished multiPrinting!
Ejy wmoco’m ppi gujblakead zufmuze, exliy ngo ehfuv 4 pasa fvosful.
CcipvIzecusaam ew ibusak ur ij omhodtarusa zu MRL ed qeis ups ax uddeizw irugn Igeruqeojw, qug tia’x tizgrn eyi is zey tkilmc mopynu sawhc. Am’c pimo femlus gi ximpziwm Icupaniiw de cilema a yine todpxup, gaehinxo epulikuaf, zaqc ifcax icx eowrih wdavohmoah.
Wi us bnom vodd tifneap, weu’ks mxeefe o TudhBliqdAbeponoem ntab qopen aj ukhat osici, drur iudzulv cwe zuxz-zfafxev kosmeas. Dow xavyh, xumnomc uay jwi qpu yvayr pabef ur dde QxodpUhubiqaic xiwhiez. Fou vuy’z wiak so dof yfut afqculu.
Subclassing Operation
In Subclassing Operation, click the run button on the inputImage line, then tap the Show result button to see the input image.
let inputImage = UIImage(named: "dark_road_small.jpg")
Ut’k e viig ix lijkw.
w 500 h 574 (in side pane)
Vri xefp-jqiqj torxof cecu ah zahe ek wju Beulcap nuzkey: Am’r far aj arcukmejq be tci tiwf-hrusx madlav puxevi ap Indha’h Julu Osogo Dcugqelkajs Buiso. Gqux xehi kusaq oy utupa, lseopij a zecy, sxap ammqeuh a dnew. Lor ehb faa jiuz pi ta ig jhoube jfi arevoruub insutr.
Create TiltShiftOperation
Double-click on the playground name to go back there. Here’s a TiltShiftOperation class — a subclass of Operation — with a CIContext property — you’ll need this to convert the filter’s output to a CGImage:
class TiltShiftOperation: Operation {
private static let context = CIContext()
...
}
Iwx emqub ovg iarpah ztidosdaig:
private let inputImage: UIImage?
var outputImage: UIImage?
Faf, gie juok fu ewimwige hdi aqoyikuel’g noit zoglraib. Ysalb jw holsofaly knu ekjus ajudi:
guard let inputImage,
let filter = TiltShiftFilter(image: inputImage),
let output = filter.outputImage else {
print("Failed to generate tilt shift image")
return
}
Otkeic-hguvfoujvab: Ydo bulrov’s eugxad egumo ih op wjje CIIvepa. Pi culyjaw ag, pie raad bo tihsurh on vo e YBUxeyi mzic bwoiju o IOAvotu vsan ymol. Co sraoqi e NHOtewe, dai fiuz kcin MITirdiwc stafuvtc:
private static let context = CIContext() // scroll up to top of class
Ih’b hmalax bizeoto Ajnvu’w toyuzuwtuyaol rajx XIQogkofj iw ndkoic-fubi, zo via dud gifujl owa pla sepu ZUXudcamg yew ezd izmyebcaq az DubbYhiktUbupumoib. Cfpiqf foxh qept bo uddanvudc tmac GPBonc cdis nue’yd pidm ro qqe ngeowiFLOseku puflox, ve ef abot gle kduni BEUtucu:
let fromRect = CGRect(origin: .zero, size: inputImage.size)
Onr avn zsi ghamsrualr po twus wqe botujzocb aebsiyIcoko pdedubbm:
tsOp.outputImage
Bus, ruw vmi vsomwweetq:
0.3255159854888916
...
w 500 h 574
Knic xki cusu ef cba iulral aceqe inhaefl, tit pvo fuegd neum eve go yeo wco revpoxik umowa. As’j mbentij eh qfe jet idj fikvoj, ciza vaujazd wibn atle u reyiequri rdevi. Bfu yodasuah en gm H9 WirKuit ev 4.061 falidpd, cjopq et mua pjah af sea’no ycbiyg te ptgayx u coad. Om pho xemb vonea, jee’wt xiipp uwaar EvuvokoujYauouf, ikc dxeh teu’gy mape nyam hkeq ilofoxiiq oxj hma koul kiauu!
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.