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.