What does the property "Nonatomic" mean?

What does "nonatomic" mean in this code?

@property(nonatomic, retain) UITextField *theUsersName;

What is the difference between atomic and nonatomic?

Thanks


Solution 1:

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 @synthesized 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.

Solution 2:

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

  1. atomic //default
  2. nonatomic
  3. strong=retain //default
  4. weak= unsafe_unretained
  5. retain
  6. assign //default
  7. unsafe_unretained
  8. copy
  9. readonly
  10. readwrite //default

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!!

Variable property attributes or Modifiers in iOS

  1. atomic
    • Atomic means only one thread access the variable (static type).
    • Atomic is thread safe.
    • But it is slow in performance.
    • Atomic is default behavior.
    • Atomic accessors in a non garbage-collected environment (i.e. when using retain/release/autorelease) will use a lock to ensure that another thread doesn't interfere with the correct setting/getting of the value.
    • it is not actually a keyword.

Example :

@property (retain) NSString *name;

@synthesize name;
  1. nonatomic
    • Nonatomic means multiple thread access the variable (dynamic type).
    • Nonatomic is thread unsafe.
    • But it is fast in performance.
    • Nonatomic is NOT default behavior; we need to add nonatomic keyword in property attribute.
    • it may result in unexpected behavior, when two different process (threads) access the same variable at the same time.

Example:

@property (nonatomic, retain) NSString *name;

@synthesize name;

Solution 3:

In addition to what's already been said about threadsafeness, non-atomic properties are faster than atomic accessors. It's not something you usually need to worry about, but keep it in mind. Core Data generated properties are nonatomic partially for this reason.