When defining a property like this — where you’re using a class, like NSString
, that has a mutable subclass — it’s vital that you mark the property as copy
. Without that, the property isn’t safe regardless of threading O-:
Assuming that, a property marked as atomic
is… well… atomic. That is, you can read or write it from any thread and you’ll always get consistent behaviour. Specifically, a reader will always get a value that’s either the current value or a value that was current at some point in the past.
Is that “thread safe”? It depends on your definition of that term. My experience is that folks come to concurrency from different backgrounds, and their understanding of the term thread safe is based on that experience.
My experience is that atomic properties are rarely useful. Consider this classic example:
@property (atomic, copy, nonnull) NSString * familyName;
@property (atomic, copy, nonnull) NSString * givenName;
Each property is ‘thread safe’ but that doesn’t mean the object as a whole is. Someone who reads both properties might receive inconsistent values.
Now, rarely useful isn’t the same as never useful. Sometimes an atomic property is the right tool for the job. But, in general, my experience is that it’s better to manage concurrency as a higher level, which is what we’re doing with the Swift concurrency model.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"