Home > DeveloperSection > Forums > Difference between atomic, nonatomic @property declarations
Anurag Sharma
Anurag Sharma

Total Post:40

Points:280
Posted on    November-15-2015 11:11 PM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 364  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?

 



Tarun Kumar

Total Post:213

Points:1635
Supported
Posted on    November-17-2015 10:57 PM

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;


Don't want to miss updates? Please click the below button!

Follow MindStick