DIFFERENCE BETWEEN ATOMIC, NONATOMIC @PROPERTY DECLARATIONS

Anurag Sharma

Total Post:40

Points:280
Posted by  Anurag Sharma
 697  View(s)
Ratings:
Rate this:

I have a confusion in @property declaration between atomic & nonatomic?
For example:

@property(nonatomic, retain) UITextField * countryName;
@property(atomic, retain) UITextField *countryName;

What is the operational difference between both statements?

 

  1. Tarun Kumar

    Post:214

    Points:1642
    Re: Difference between atomic, nonatomic @property declarations

    Here is the difference:
    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 will use a lock to ensure that another thread does not interfere with the correct setting/getting of the value.
    - it is not actually a keyword.
    ForExample:
    @property(retain)NSString *title;
    @synthesize title;

    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.
    ForExample:
    @property(nonatomic,retain)NSString *title;
    @synthesize title;

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!