Atomic and Nonatomic properties in Objective C
What is the difference between Atomic and Nonatomic properties in iOS
This has been the favorite question in iOS developer job interviews for many years.
And it is also the first question a beginner iOS developer ask once he sees the declaration of a property in Xcode like this:
@property (nonatomic , strong) NSString *firstName;
The short answer is:
atomic: properties are atomic by default, it is thread save but slower than nonatomic
nonatomic: not thread save but faster than atomic
Properties in Objective C has a backing variable and the compiler creates a setter and a getter methods for you automatically.
Now imagine that one thread is calling the property setter to modify the variable value and another thread is calling the getter to retrieve its value at the same time.
If the property is nonatomic then you may get unexpected results but atomic will insure that a value is fully retrieved by the getter or fully set by the setter even in case of setter and getter methods are being called simultaneously by many threads.
If thread A and thread B and thread C are calling the setter and Thread X is calling the getter then using atomic properties will ensure that you will get a full value in thread X. But is it the value set by thread A or B or C ? you will never know.
Data integrity is not maintained by atomic properties
I recommend reading Enums in Swift