What does "nonatomic" mean in this code?
@property(nonatomic, retain) UITextField *theUsersName;
What is the difference between atomic and nonatomic?
Thanks
What does "nonatomic" mean in this code?
@property(nonatomic, retain) UITextField *theUsersName;
What is the difference between atomic and nonatomic?
Thanks
Take a look at the Apple Docs.
Basically, if you say nonatomic
, and you generate the accessors using @synthesize
, then if multiple threads try to change/read the property at once, badness can happen. You can get partially-written values or over-released/retained objects, which can easily lead to crashes. (This is potentially a lot faster than an atomic accessor, though.)
If you use the default (which is atomic
; there used to be no keyword for this, but there is now), then the @synthesize
d methods use an object-level lock to ensure that multiple reads/writes to a single property are serialized. As the Apple docs point out, this doesn't mean the whole object is thread-safe, but the individual property reads/writes are.
Of course, if you implement your own accessors rather than using @synthesize
, I think these declarations do nothing except express your intent as to whether the property is implemented in a threadsafe manner.
After reading so many Articles and StackOverflow posts, and having made demo apps to check Variable property attributes, I decided to put all the attributes information together
so below is the detailed article link where you can find above mentioned all attributes, that will definitely help you. Many thanks to all the people who give best answers here!!
Example :
@property (retain) NSString *name;
@synthesize name;
Example:
@property (nonatomic, retain) NSString *name;
@synthesize name;
Usually atomic means that writes/reads to the property happen as a single operation. Atomic_operation
You can able to get a handle of this stuffs by reading the below article.
Threading Explained with the nonatomic's purpose
nonatomic - Not Thread Safe
atomic - Thread Safe - This is the default property attribute.
The "atomic” means that access to the property is thread-safe. while the "nonatomic" is the opposite of it. When you declare a property in Objective-C the property are atomic by default so that synthesized accessors provide robust access to property in a multithreaded environment—that is, the value returned from the getter or set via the setter is always fully retrieved or set regardless of what other threads are executing concurrently. But if you declare property as nonatomic like below
@property (nonatomic, retain) NSString *myString;
then it means a synthesized accessor for an object property simply returns the value directly. The effect of the nonatomic attribute depends on the environment. By default, synthesized accessors are atomic. So nonatomic is considerably faster than atomic.