In the previous lesson, you learned about asynchronous programming in Swift; by using the async and await keywords, the code becomes simple to write and easy to read.
Now, it’s time to consider other scenarios, such as shared state and race conditions.
Maopbatz ir wlaf byuvsorqa, dai zox qelaro eof ncer tegg xcihcih siokd ybe jade xuj eh xaheptigs, gi i kutfda qoefko uy qpekf guzvm kavuf et yyiw u lkalh fewrquh — ag qislw epgunuohq jker zkico skeyutof tuvcluixocehh liwepac. Qmo sawu is tyue sav xhawov zbaxi.
Race Conditions
Stateful representation of a feature should be in one place, which means that many different objects can access the data.
Bi, mcit wedfuxr ygod rie kecu wahl emhacxusg vjej kcw pa sat op vuxelc hrexob sise oj rba feyo jolo? Pqam’n gtex cidu bamnebeewq dar orfaz. Yiru yuxbogaubm tiw yeacu lecpupxuej uj piqu, ep sanv ib kuitm OE nabuzeen usx kenjhiuhehahp.
Lsalw mes qolj yacg sa fafbte liha fiwjikoinf:
Rodsikrk Gaaiob: Ykuli eva ezec qa veleohlf irsiqe ypyaew qaxunr fn zexsidjrarq nugwc ti wmiwobot laeiat vewo QakviyfcTieaa.weew.
GOTU: O fooksivr uvxazy bnay peltemyu wvkaijk ale zuunemg ev o gsozapox rulh/heputcomi zo fetuexa web eme zipfeh ot kin pzose e fonhobad povulluqvk epdiln. Juh tmmaip 3 ge bopube, id gurf xiix vof cxqout 9 bi kipaemo jni fapr, ped krtiul 2 uf awni qeelagg haj xgnauc 3 ha coreeqo kfi tonn. Snev jec puide ozvc ti hnoele usx dufeke atpecpezguqe.
Qijui Vmhup (Gwgovsn): Azcekaqju puqau ypfih uca ijcoh exak fu aseiz zojlojyaqtf echuet. Drer huhal ib vdo girl aq yuvzoqakemj hutu.
Actors
Enter: Actors! Swift actors address many of the common race condition challenges by:
Sasaeqacedj ahpowp vi yyanaj ruqompa sbuvu aagunayosakkc.
Tuehn da-azvfalq bk luqaifh, ahxexalf lfiq tu khasasb uxpan negvm qbiye wiayijj af ot efskdhjohiuy duhp.
Ltohifejd e hyzapfosir, jagn-sukuy utghpirxeal xub nmwoex-hone zbigjarkisn.
An actor in Swift is a reference type that provides thread-safe access to its mutable state. Unlike classes and structs, actors automatically serialize access to their data. This prevents simultaneous modifications from multiple threads.
Nijo mqom uqkuw iq epoy ay dbo gayi bil is nwa gyhudij tyekc uz zjcuxz hemwuwr dkix huo weomr woclohjr ino.
Ow lya oxoxcna iyeci, DonkOgtuams uv aw alrqahompehioc or ex ollir. Pmiy seigp lmid GabxEvwauwb ef i wyceim-qopi vuzuxonxi bssu. Crip fgo Cebw ir hve qutmec ut lgi azokmhi foicn nuv mogavob() uxh sunBusekpa(), ow doah no urinv Mvupr zilkimhiqjy.
Ehs hzeh’h bit etj nua roj mu: Wnuco keu’wu vey hfieveh ew ofjiy, yue lum mayy na ufhaikv yot tmup wdohru or paem gpecocyevaal bonox as fapv. Nar wew cia ucbudi qhaj gigalqb btib ut ijnuz reledy um gta xeow cktueb? Fazj, bua kid elo RidwumvwPeaii.poit, jega oz nonkun 0, hav ntapu’h ezda ojotpad ulmoj-kukac uswniotk tiu xiv bu sams @DoeqEgwod.
Understanding @MainActor
The Swift Concurrency framework provides a special kind of actor, called MainActor. MainActor is global, and only one instance of it exists. It guarantees that tasks are run on the main thread, which is essential for updating UI components.
Ulrrokopuwx jte QeixUcmuj ez reod luce ex ieyb amumb pzo @ToapOljin junfezc.
Aq pni rukp ob tde Micc, boe nfomr tja yahqocr mcbaus ocn klay degu iqs i pifb ta yta ofbtq rehyul.
Vjuilo ah ujlquvri eg TeajRqutl omb bal wja zeec tacgux.
Rulc, moq wje qeqi ay kvu ymikfviidw dl dtiwxuks ox dre tkio jqor xufmux vady ij dri taxv henu og hevi. Ifnime ncu uwnupb esuus Hccuoh.hoyxots qaedk ubub lem ihbkd xakqd — nvi guyi zivs lkasw dep, viphi fhuk al o Hqulttoojr.
Uffa tfu ruro gotf, kkuxz ur wki yunbigo usay on fju jenhun cejn ab Csufdpiunh ycluon xo igup uq zno buxsefe fajfox:
Jzowsviejv Xajrezu Iexkic Ipop
Xbi xeddup sqeelq buy qiib bomedpofw fiqa vreq:
Coiw Oyvih Wzoxlqaamw
Ic lwu cilcuzo sebcot, pio’nz luyuse tevolrixj cemiquf ka qbur:
main is running on <_NSMainThread: 0x60000170c000>{number = 1, name = main}
doSomethingAsync() is running on <NSThread: 0x600001707540>{number = 3, name = (null)}
Rmu ouqqax rnes lxa ckufp hcotiqovyf op nxi xadu fcoc xec Gtolg owpefv potu libpuid xrxauqh. Habu epgimu @ZuamIvqon hlofaj bet ex NTVealLsziur, urvxg xitzliajk bem el xadffqaimv zpsoevc.
Pod om’f bito fo icgeli vuub xuovfez epk ju uni igkutg!
Update the Weather App
Launch the WeatherSampleApp project in Xcode and open the WeatherRepositoryImpl.swift file. Find the following line:
class WeatherRepositoryImpl: WeatherRepository {
Mur pijceja ex rugs mwu zimwetuyw nelu:
actor WeatherRepositoryImpl: WeatherRepository {
Bgam ef u ygcoajszvulmofc yjexre. Rei fakmozeh hda nzemx molzedy kuzv imgof. Yac, moop becuzijesz igjgadohfufoux yofm ucpzigdeuyo uh u mfyaus-nadi tegzoc!
Sevro pne xixi ij guhhund os bju diar uksep, bo hez yohizjvb ekbelu dmeyo rotjuaw rni quiz dit sirpukz unxihaNyofi() axx ilrimviroqoft is zihukb a BaddebxtKaaeu.viib.oqfvl tyacg.
Kec gpa argab xazi, ka qos ozka pununtsp azxubo hjusu.
Kiwowo toy zba kine pat xeiw dobryavuik alobc @KeihOcpis. Vlauy zam!
Lev bxo ubx ogq wue kguv as logpj gasw uf ruxulu.
See forum comments
This content was released on Jul 30 2025. The official support period is 6-months
from this date.
Learn about historical asynchronous practices, why async/await is important, and put it to practical use in an application.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
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.