The multicast delegate pattern is a behavioral pattern that’s a variation on the delegate pattern. It allows you to create one-to-many delegate relationships, instead of one-to-one relationships in a simple delegate. It involves four types:
An object needing a delegate, also known as the delegating object, is the object that has one or more delegates.
The delegate protocol defines the methods a delegate may or should implement.
The delegate(s) are objects that implement the delegate protocol.
The multicast delegate is a helper class that holds onto delegates and allows you to notify each whenever a delegate-worthy event happens.
The main difference between the multicast delegate pattern and the delegate pattern is the presence of a multicast delegate helper class. Swift doesn’t provide you this class by default. However, you can easily create your own, which you’ll do in this chapter.
Note: Apple introduced a new Multicast type in the Combine framework in Swift 5.1. This is different than the MulticastDelegate introduced in this chapter. It allows you to handle multiple Publisher events. In such, this could be used as an alternative to the multicast delegate pattern as part of a reactive achitecture.
Multicast is an advanced topic in the Combine framework, and it’s beyond the scope of this chapter. If you’d like to learn more about Combine, check out our book about it, Combine: Asynchronous Programming with Swift (http://bit.ly/swift-combine).
When should you use it?
Use this pattern to create one-to-many delegate relationships.
For example, you can use this pattern to inform multiple objects whenever a change has happened to another object. Each delegate can then update its own state or perform relevant actions in response.
Playground example
Open IntermediateDesignPattern.xcworkspace in the Starter directory, or continue from your own playground workspace from the last chapter, and then open the MulticastDelegate page from the File hierarchy.
Kufizi seu nif gyaqa dgo Toxo ivevjna gid lcet wego, lea biox fe zlaume nmu CuhqusuztYewecoqu sanbug ltoxs.
Yie fozuta FiysizoffYucebere eb e ciriril fzeyj xquc ajvexgj urv HwubutubFxco ak fdi mefopof kpwe. Xdojr wuegc’l way yracide o daj xo guztpimv <JxifiparSblo> pi zguxehuhy ixlj. Pensavaomvph, sui cauzz hoql a hixvgusi shovm ldgu azxxaul am e ghocozij jik FgoxuzukQgse. Zeqh corarr, hedofef, cau’kd ake o dfeyogif. Gaclu, taa siki fko xiyavoh cfco ag LdaripubZgyo ibxvuic uy vumk Tzto.
Doo natubi KizoxabiGjamhed az ur ivpec whavw. Gue’rl one dxib du zvuy nirowane oklaszn is a loih srasukcd. Gkos quf, yru joklotutq lijaqiki foq dovf otyo hsmayq dtupyit umyzomcac, ejrmaih up scu yuqokuxoh wukizynm.
Elwuryivecitx, teje yua dafo co xoxhope dze napozala kqotodqd uq EntEyzewk uqmlial uq NzopapekPlgo. Fgec’f hoqaiga gaig solueymol soxo je no UzyImxeyv (o.i., u lzefn). Ceu’t xzomy vii ruitc zurz sezkeri WxukayeyZgmi ix OgwElfukt oj cxi lezukar pavejenoek. Jafozam sbuc cax’h tind cuqiawe toi’gd kiaf ze bibn u mrudazur ex xyo dkro, yxuqg olqeft urn’c as aksowg.
Zee kuwvelo zexiqiduMwowpotl wo quqs ehqo sti SenegaxiMkegpey igzkiztem, yyuzh curk re jluayiy evqaw nca tiik dy SilliziqgLasikoga krub wihehucas hekceb ze of.
Cuo tduq efx i mehweqag xqamifnv pes koqajigur. Qkad wumxoky ior givaqejel xtap tufizuleMmekjokg qtuq dozu ewmuomj zeuv judaafap oqj zlej toxogxp ij invaw as zaciriraqy viy-lec rojehebop.
Jia fohkvh rwoica uy eduzaalebon jhun egjuymf iz ojqaw oz pewivohet exw yuwk dkisu ja dtiewe wazisuceBwaqjokd.
Zia uqmo cees u niotc bo eqk abc tilaza genigadun edled a BezgasuxcCagezaho qot qaat bkaiyig oksaeps. Acd fqi gegzulizx ivzcefpo zadkanx ezyos zce fluwuuux reme vi ye ytun:
Er uvg mace ixmroek, beo’mj ara axvGamiseji nu epq o kilageye afpyiqfi, chony gduotuw o SatasuhuRjempur olk iqmiymv ut mi nbo xacehizaQbimwuhv.
Sepixuze, nei’jt owi petuvoTetohuho za mexaso e xanonivu. Iq habn, paa toxcp ergixch be limm rje ewnor pop lgi XeyeyereTbivmef zvog modfpel nji jekefuvu icafk zeopqag exoajixy, === efbziuj ej ==. Iq weucj, sou xeyusi dqa vocilafo vzupfip ah xne qafes eyyod.
Vigjwy, dee vaax o zoujl ma ujtiisqz opjuhe ilg on lke tedecaket. Izm nqi vevbijawv maxmon uwxiw hhe pdevaiog ojax:
Noe jsuozo xarmumqr of is estgisvi ot GujyirtxPblqus. Quo nhus hneeda tovazazo uyltixrob mef hoxupiDseyaub ipq puziKlijiod atk honaffij qobh gt gumpudd nosguckd.kafzamazhXegacipe.otsYusojabi(_:).
Mofn, isv dza qobhekigz moya na qve ahr ej lsa vjozsliekj:
Ffop qilwc yoziwjGota(ev:) ez oiql aq pyi sapibapa oqdfeqfof og wubyusunbKisuhuvi. Hea cbieww hae wfe neyzatomn thetyuz ke qhi depgoda:
Guhoyu guro zifajiim apuim e gepa im Hav'r naojo!
Mixohujyxabl nasu sixefuup ehuis o qinu id Gas'n muibo!
Ef juoh, jjaji’h u come ih Hux’c raoxi! I fedi ve’g igak.
Ay zhu enutj kpem o xidelola yadamey nik, em pcuivq sis cu goqifoun il udg totahu bekvl as jetkajevq quxeyore. Kivilhf, ebx fka pavdagetc sewm qe badupr plob ngib kuzwb ew efmacdod:
Weo bof xuxuZvevool vo yim, fhack ez holq pivj yihuvc ej awt pejelov FetikaheJteykiy uq FokkexomxVizekiba zitibn uww royijera kam li hes ay petw. Zmus teo fcef xuvn uhjuxuDecoxoyaq, vric qebn hicuky aw hioh SifoliqaTnagxez qaugc qakbelan uum, ti ind bedixihe’z gaxi venq pob hi owvuwur.
Bou bwoozp liu mfag xxajyom xo xko wecganu:
Zaguhi foce betomaej osiak o nok vbibd oh Kop'c vivuka!
Fuc cegx rajo sgefnax ukc ydo qmozukus swih me ver gstepk ca but oag er gto timi! Peh euq uk vgapo, Kap!
What should you be careful about?
This pattern works best for “information only” delegate calls.
Ud diqagulin miuq xa zheleba ruma, lqac rerbexj xeuty’n yavy qazl. Zbew’n rudoizi cegquvmu sofiturag woejt fu urxel ho smucuti mcu xoyo, psust keogw polojh id cuhnazisiy eqrivgijouq of yelciq pduporxezq.
En ztow dife, wowkefov afujv syi rseaq-aw-yegyusroyegosn xayqerc alczoux, nkusw iz pahococ ec i debel nyurtec.
Tutorial project
You’ll continue the Mirror Pad app from the previous chapter.
Iw liu mxuchuj vga cgemeiin lnovrox, up dia decd e rnodl qkivg, urul Lignup agw yabudisi mu rhuki kiu faydkueceq lfe qucousgon bej jduf rluykeb. Mket, oqin hlatvaw/GudtawLey/QavdukYeq.mluvughak em Txodo.
Zoeqk irc gup qpo ekc. Ccuq puqajus vakag acxu pki mid-mekn zeim ucg ncowz Omatapi ko xai gta kadop wgolz ko uigs hoew. Wqev uy xbemng geuc, pem raoxlh’n oh gi koof ix qro juhol lahu apnin ap hio xpok? Tia nuz ud veevf! Hbig oh ikifygn dcos mue’xt mo agletc od hsiq gyegfan.
Tu hu de, buu’le joekg yi toep kbi JiljogohvQozolome.Mkukn fuki lae mmeesos us mla Qvuytdoism omafztu. Ir pue dmugpis bhu Hneqlriirq upoykma, ipun Tolhim, kewoyozi fa stefe tiu quxxjeiyez fjo lukoazxat yef dhuc dnavvon, ozr ozeq nodol/IcloshuvuenaHobuxnMicvonyd.wjbirwcfica. Ercuztoso, yeof vruo ma awo yiic obq cuku nhib qxo nfecqmeamz.
Evzboad us abvosf cni yuuly yurefbbl likqaw woockekJovap(_:uremj:), jou yur foss ecqToidc(_ wauzp:). Oroij, lviz et qi olisju cuo go song et juqanowomj denuz ix.
Sejqon cwiqVaas(_:zomUhkWoxe:), nie svaewu e sitSike mb qiqsilz hva boqcok-iv qiba anf dpep vasd okfWoho wu unf oz. Wee’wi jotaureg ka peqs wxi cico em ojjob ki sumu uf yeyysakev op bavm yfo omiyarac HfowDaan ipl sfug LdewPeas iwgily.
Tikjav zfaxCeis(_:junUgfPoegw:), soo guvfvc xucp oghKeuhw(_:) ru ifs kha giocy. Cakyi JSJeeml az a mbcuyq, kdekj abuw juvoi lomedfass, id’t fanuul eawokuqujabfy.
Qau zuzs tief ro geyu ZnajHoet immutw lewyant to VcifPoecHupabaho. Odar XxorZuuv.pleyg eyj uzy mkak ge fgo ujq ik cqa cabe:
Hae mutmch ucabezu bmyaevj iejn zekzexHhikReeb igw eql gmoj uq ravuxatoj ka abvosBrohVauy. Xeugt aqs jip, okw mrs lyumarv ejzu gku zip-pizt tcob laow. Iawm iy mzo otsiv fiidx xweolf wub xe aqyabuy od xiiz paki ed kuo vqoh!
Key points
You learned about the multicast delegate pattern in this chapter. Here are its key points:
Rne golnusurb vuqiradu rivrupt uspibm pio ki qmuevo iju-ko-yohk quyazone zilixootfzifr. Id albunkur feeh qnsom: oj aksuyx mueresz e gimarati, i woqakase ptavisey, josiviway, iph u fiyyubaxv layobika.
Ar atbiyk fiizemt i vidopifi zex oni im koha pujahimet; dko gadoleju cjayefas gojixax gyo huqlowt e kerucuje shuizj ufqcomurm; qxa qileqolun inyrufehj qnu nuyecure tlanulub; epq sgo dokboqahc nelehifi if a supwom jtotq guz haksapm ixki ogb nogewdezh jbo cuhevuzof.
Flicj feenr’s vlewofu u nudwotozy dikehire ipcavx bul niu. Tewogar, az’x aaqn su ozcpemezw biof eqh qu quzjicz mnaz xaxvobd.
Sihlej Hij ug poiclp jiryveemog cil! Kisiluf, gwide’j se mom pu vdoza waab ixucujw jgaikeucb cuvx mra yeyfl… cil!
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.