Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Atomic vs nonatomic properties in Objective C

What is the functional difference between these 3?


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

Answer :The last two are identical; atomic is the default behavior. With atomic, the synthesized setter/getter will ensure that a whole value is always returned from the getter or set by the setter, regardless of setter activity on any other thread. That is, if thread A is in the middle of the getter while thread B calls the setter, an actual viable value -an autoreleased object, most likely -- will be returned to the caller in A. Atomic guarantees that access to the property will be performed in an atomic manner. E.g. it will be thread safe, any get/set of a property on one thread must complete before another can access it. In nonatomic, no such guarantees are made. Thus, nonatomic is considerably faster than atomic.

Below are some examples of what is actually happening in atomic & nonatomic Atomic :
//@property(retain) UITextField *userName; //Generates roughly - (UITextField *) userName { UITextField *retval = nil; @synchronized(self) { retval = [[userName retain] autorelease]; } return retval; } - (void) setUserName:(UITextField *)userName_ { @synchronized(self) { [userName_ retain]; [userName release]; userName = userName_; } }

nonatomic :
//@property(nonatomic, retain) UITextField *userName; //Generates roughly - (UITextField *) userName { return userName;

} - (void) setUserName:(UITextField *)userName_ { [userName_ retain]; [userName release]; userName = userName_; }

http://www.otierney.net/objective-c.html#downloading

http://vgable.com/blog/2008/11/14/prefer-copy-over-retain/

http://macdevelopertips.com/objective-c/objective-c-categories.html

You might also like