Actors in Swift

Swift’s new concurrency programming takes us to the next level of coding styles.

devCracker
ITNEXT

--

Swift Concurrency — Swift Actor Introduction
Photo by Simon Hurry on Unsplash

Actors in Swift is one of the cool addition in Swift's programming world to handle data race and concurrency issues. Before moving further into Swift Actor, we need to have an understanding of data race and how Thread Sanitizer helps us to find it. Data race is something happening when the same memory is accessed from multiple threads, e.g. one is reading the value and another one is writing the value. This data race leads to misery crashes and unpredictable functional issues which are hard to debug and solve. Luckily, Thread Sanitizer in Xcode helps us to find this data race and solve it by updating our logic or code. If you haven’t heard about Thread Sanitizer, please take a look here at my simple write-up about Thread Sanitizer.

Swift 5.5 came up with many good additions, let’s talk about Actors in swift in this write-up in detail. This would ease iOS/Mac developers' life to handle multi threading use cases efficiently.

How it was before to handle Data Race?

Before Swift Actor, to handle data race we need to either change our design or implementation or we need to use DispatchQueue with locks to avoid writing reading happening at the same time. Here is a sample class where we are handling the data race with barrier lock.

Handling data race in swift iOS. with DispatchQueue and Barrier flag
Handling data race using dispatch Queue and locks

If we read the above sample class, there are quite a number of things we have done to avoid data race. We have a queue that has a barrier flag that stops reading while it's being written by another thread. And we have a private property also in order to synchronise the update or read. We would also have some questions in our mind, like, which lock we need to use, and the number of queues for number of properties?. Actors in Swift is making our life easier from iOS 15 and Swift 5.5.

let’s see the same use case with Swift Actor. It's simple and clean. Isn’t it?

Swift Actor sample class and detailed tutorial about actors in Swift

Quite a number of codes are simplified here and no dispatch queue with locks, which makes us write the clean code and understand it easily. Behind the scene, Swift Actor forces the compiler for synchronised access by having optimised synchronised process and prevents us from not introducing the data race unknowingly. Okay, let’s understand or know more about Swift Actor.

Note: If your application supports below iOS 15, you need to have the fallback logic to handle data race, As Swift actors is available from iOS15 only.

What is Swift Actor?

Swift in actor is same as class or struct, we can define an actor with a keyword as “actor” like below.

declaring actor in swift.
swift actor declaration

Okay, here our mind starts asking this either actor is a value or reference type. Because that plays an important role in data race. Actor in swift, is a reference type like classes. So, whenever copy gets changed, the original will also get that change. Then, what is the difference?. Yes, here are some differences between class and actor. Actor does not support inheritance, hence we will not have override, convenience init and all.

class vs actors. Difference between actors and classes
Swift actor do not support inheritance.

However, the biggest difference is isolating the data access.

Accessing data from Actors:

Okay, now let’s see how we can access the data from Actors.
If we try to access the actor as we normally do, the compiler will throw an error as below.

error on accessing the data from swift actor
accessing data from swift actors
error on accessing the mutable property from swift actor.
accessing Actor’s mutable property

Because we will not be sure when the access will be granted to read/write. Hence swift comes up with new addition async. So, we can use await and async, it will serialise our access to that data. So easy isn’t it :). This error will come for computed properties as well, but wont come for constants as data race will not happen for constant properties

Isolated access vs nonisolated access?

Swift Actors has two types of access isolated and nonisolated access. By default, all access in actor is isolated to prevent the data races. we may come across a use case like if we want to have some functions which we are sure that it wont have data race. Then it does not make sense to use async/await over there. In such cases, we can mark the method as nonisoalted.

non isolated access in Swift actors
nonisolated access in swift actors

Wanna learn more about @MainActor in Swift? please checkout my article to understand @MainActor in swift.

Upgrade your medium membership from here to read good amount of article and scale your skills.

--

--

Writer for

professional mobile app developer | iOS | Android | GraphQL | Typescript | nodejs. Sports and musics fuels my mind to get back to development