Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should all properties be declared "nonatomic"?

I am wondering: should all properties in iPhone development be nonatomic? If so, why?

like image 636
nyanev Avatar asked May 24 '11 19:05

nyanev


1 Answers

From The Objective-C Programming Language, obligatory guide:


Atomicity

You can use this attribute to specify that accessor methods are not atomic. (There is no keyword to denote atomic.)

nonatomic
Specifies that accessors are nonatomic. By default, accessors are atomic.

Properties are atomic by default so that synthesized accessors provide robust access to properties in a multithreaded environment—that is, the value returned from the getter or set via the setter is always fully retrieved or set regardless of what other threads are executing concurrently. For more details, see “Performance and Threading.”

If you specify retain or copy and do not specify nonatomic, then in a reference-counted environment, a synthesized get accessor for an object property uses a lock and retains and autoreleases the returned value—the implementation will be similar to the following:

[_internal lock]; // lock using an object-level lock
id result = [[value retain] autorelease];
[_internal unlock];
return result;

If you specify nonatomic, a synthesized accessor for an object property simply returns the value directly.

like image 57
sidyll Avatar answered Oct 22 '22 20:10

sidyll