@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 mean atomic
?
When indicating in the properties
atomic
ornonatomic
:Atomic = thread safery.
Nonatomic = no thread safery.
Atomic = less efficient than nanotomic in terms of speed.
Nonatomic = more efficient than atomic in terms of speed.
when to use:
Atomic
-> when it is in an environment that handles multithreading and these can be accessed from several threads.Nonatomic
-> when you are sure that it is not going to be accessed from several threads and these can generate inconsistencies in the data with their accesses, either when applying changes to them or when recovering them.If we make a method call
get
for a property to get the object's values, and a moment laterhilo/Thread
our application makes a method callset
for that same property.Applying the above in a context in which part of the object has been read, and modifications have been made simultaneously (at the same "time"), it is possible that the result we obtain in the end has errors in the data or is inconsistent, depending on whether this is
atomic
ornonatomic
.If atomic has been specified in the property, or not (since atomic is the default option), it would be guaranteed that the method would
get
obtain all the original data, before another onehilo/Thread
made changes to said object.It is possible and it is achieved by stopping the process of the call to
set
in until the executedCPU
one is finished .get
Another difference is that nonatomic does not stop the thread, which does not cause pauses in the
CPU
application and the performance of the application is not affected by it, making it "faster" with the use of nanotomic.Atomic and Nonatomic
By default all properties are Atomic. This means that accessor methods ensure that a value is always fully set or retrieved, even when accessor methods are called simultaneously from different threads.
You can use Nonatomic to specify that accessor methods return a value directly, with no guarantee about what will happen if that same value is accessed simultaneously from different threads.
For this reason, it is faster to access a nonatomic property than an atomic one.