Thread Safety in Swift
I have been running into some thread safety problems recently. In Objective-C, properties can be atomic and the immutable structures are thread safe while mutable structures are usually not thread safe Ref: Apple Docs. But I did not really find official docs for the Swift on thread safety.
Here is a summary afer I looked into the thread safety in Swift.
- Swift Array and Dictionaries are not thread safe. Do not modify the data source of a tableView in a background thread. Otherwise, there is a risk that your app may crash. Just keep in mind not to access the same array or dictionary from different threads. If you really have to, create a serial dispatch queue and make sure all operations on the object happens in serial.
- Swift objects are not thread safe. It is actually unsafe to read and write to same variable in multiple threads. There is proposal but Swift does not support @atomic yet.
- It is possible to create thread safe Arrays and Dictionaries using locks or semaphores. There are a bunch of tutorials on this.
- The key takeaway is that we should keep in mind to update the models in the view controller in the main thread.