Difference between atomic, nonatomic @property declarations

Total Post:40

 1007  View(s)
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. Post:214

    Re: Difference between atomic, nonatomic @property declarations

    Here is the difference:
    - 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.
    @property(retain)NSString *title;
    @synthesize title;

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

      Modified On Apr-10-2018 05:52:54 AM