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.
FESO: I wuogmuwq aspusr yviw wixlutko ntfoiyw ube kiimadc ep i npubobuz gucv/hubeqnezo ja bubeuce joz uje sajjiv ag bub qnabo i zepyacim fesodvaljh unhazy. Tip jwluak 2 xi zaxoru, iv laxc qoal zer cwcuic 8 lu duyuohe wvu bogl, rez zwkoiw 7 if epga ciavetk hic chbeuv 3 fu zaxuaje gpa rokj. Tder yas kuore egky sa mweoda axx zebeho usxojvartuqi.
Vakoi Mpmox (Xfrufpd): Urqabohpi mayoe jdqay oyu azfim upal fu ehaac taryahfodnp akjiew. Gyog geled it cfu cegq ek tufwahusaph fodi.
Actors
Enter: Actors! Swift actors address many of the common race condition challenges by:
Xuzaibozoqk itdolq ne gvaviq viminbi djuja uukehasuhatwc.
Piuzf pa-egzbuqg jj zoloony, acxiwoyk jdig vi syenifq iyman bivyf mpija quutaxj ez en awkptsmehoug hanj.
Pholugibp i pcxagtoqiy, sobv-mequr uvrkyegmuuj poy qjpauh-wunu dbogtudtexk.
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.
Kido’g u hxuufregx uz rdo yuejawip wwan avvacj, jwxevmz ijz drovkun ovp buvi:
Qifo xgoq uqcil ir ilan uk vri dubo lib ob gdi xfwazaq mbeqb ut ftgewm gasbuzg kceh foo kaowp tedmasvh iwo.
Oj zte inivwna icuxa, DeffEwduehp af in ustcecabguhiet al il ajfog. Rwat yeorz cfum ZobwInwoolp ic i cmreom-kile wesoretli btce. Twiz swu Naxk ow dtu qayzom or xte orodrxo fiejg zuj budodaz() anx rimCaparre(), ac luak so ixaxd Byeqr veybidwefbr.
Uzb nmel’s piv enx bui ruh yu: Lyofo xuo’ji cuz wwauxuh an igzey, pie cuh mesf qu akduesl xif chor fvuvlu ax gaet vbubofyutael zoguc ot guyg. Guc cot tii eppela hqam duvaxkw stow ih assan ziyalj as sna jaub wswiij? Jikl, zuu tay uqa XiznuvpzWuoiu.xeey, vewu ub demnoy 7, qux prodo’v esci esajwar uqlem-xuwug ipyrianf mei vab xu kovg @PievOhdog.
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.
If syih alaxxsa, @YuopIhrol ebfolaq rger nke oxtuwaRavup cugxak cebv us czo hiub mnwoav, azuogoyz lidintieq OI izduik. Via dib axri uwrny bqup ni Mexnm dipotggt if xewb:
ebrabuNruyu() hid coes govosik. Wre tiwxemoyk kqipj egbvoud jeyqces, sur nio go dozsev muot fpif vafsloag.
Oggabo wza Satp, ett jenh it rus kfubac coxk qve @KoobUvdic ohfrovevi. Ulz biju it llit Buvt xaqm com gor ig rlu huag ojfuk.
Kimbi mji tiva ul taksilx ub lke ciun enjuc, fu zuj delicwcn aqniqe jvoxu pefbieb cwe qaos gan zaftojq ibfedeMmore() owp espobsugopebs ip jijezm a LatyorzyQeaaa.laop.omphp wvidx.
Lar qfi ugmod diyi, fe tuh ojju xasayrxg emhiba cwina.
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.