What is the difference between atomic and nonatomic attributes?


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

What is the difference in these three statements? What does a property nonatomic or atomic mean?

asked by Jhonattan 23.01.2016 в 16:48

2 answers


By indicating in the% atomic or nonatomic properties:

Atomic = thread safery. (security in the thread).

Nonatomic = no thread safery. (no security in the thread).

Atomic = less efficient than nanotomic in terms of speed.

Nonatomic = more efficient than atomic in terms of speed.

when to use:

Atomic - > when you are in an environment that handles multithread and these can be accessed from several threads.

Nonatomic - > when you are sure that you are not going to access from several threads and these can generate with their access inconsistencies in the data, either by applying changes on them or by retrieving them.

If we make a call to a method get for a property to obtain the values of the object and an instant after another hilo/Thread of our application, it makes a call to the method set for that same property.

Applying the above in a context in which you have read part of the object, and simultaneously (at the same "time") have made changes it is possible that the result we get at the end have errors in the data or are inconsistent, depending on whether this is atomic or nonatomic .

If atomic has been indicated in the property, or not (since atomic is the option assigned by default), it would be guaranteed that the method get would obtain all the original data, before another hilo/Thread would make changes in said object.

It is possible and is achieved by stopping the call process at set in CPU until the get ends.

Another difference is that nonatomic does not stop the thread with which it does not produce pauses in CPU and the performance of the application is not affected by it making it "faster" with the use of nanotomic.

answered by 23.01.2016 / 21:08

Atomic and Nonatomic

By default all properties are Atomic. This means that the access methods ensure that a value is always fully assigned or obtained, even when the access methods are invoked simultaneously from different threads.

You can use Nonatomic to specify that the access methods return a value directly, without any guarantee about what happens if that same value is accessed simultaneously from different threads.

For this reason, it is faster to access a nonatomic property than an atomic.

answered by 23.01.2016 в 17:29