From the course: Practical Design Patterns in Swift

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Concurrency issues

Concurrency issues - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Concurrency issues

- [Instructor] I am going to demonstrate one of the biggest issues that you might encounter when implementing the Singleton. If you want to follow along with me, you can find the project in the exercise files folder chapter two, two three, begin. We'll start by implementing a method that lets up update the app settings. Let's switch to the AppSettings class and implement the missing method. I'm going to call it set value for key. It should be public as well. The value is of type any, and the key should be a string to make it compatible with our internal dictionary. And it simply sets the value for the given key. Now that we have this method, let's see what happens if we use our Singleton for multiple threads in parallel. The easiest way to simulate concurrency is via unit tests. So, let's switch to the unit tests file, and I'm going to add a new method. Let's call it testConcurrentUsage. I plan to call the AppSettings set value for a key from multiple threads. So first, I create an…

Contents