Difference between nonatomic, atomic, strong, weak, retain?

INTRODUCTION
These are very important term in iOS app development and definition of these terms should be clarify for user. User should have a good idea about what these terms actually refer to and what is benefit of them and why to use them and where to use what.

Definition of Atomic, Non-Atomic, Strong, Weak & Retain

ATOMIC
When user sets this property at the time of declaration that means it will surely return you value, means will guarantee return you a valid value and also this property are thread unsafe. At different threads can make value change at same time.

Like for example we take a CGRect frame variable

Thread A => frame = CGRect (x: 5, y:5, width:50, height:50);
Thread B => frame = CGRect (x: 25, y:25, width:50, height:50);
Thread C => frame = CGRect (x: 45, y:45, width:50, height:50);

At last the frame can return any value from above three because any thread can perform first and so on there is no perfect sequence for that so return value is not fix.

NON-ATOMIC
When user sets this property at the time of declaration that means declared variable will not have any surety of what will he return, means no guaranteed value of return. It can return you some value or partial value or some invalid value.

In above example of atomic in case of non atomic value we can say that frame can return valid value or partial value also like x = 25 and y = 45.
or x = 45 and y = 25.

STRONG
A strong property is for strong reference to existence when assigned to a variable. Once property set to strong means compiler will take care of it that it doesn’t get destroyed until user set nil value to it.

WEAK
A weak property is for weak reference to existence when assigned to a variable. Once property set to weak means any object has no direct link to that variable but still can call it and access it but has no direct reference to that.

For example :- If a balloon has many strings and those strings are in hands of persons than in this case strong reference is there persons can see balloon and can have access of its properties and call its methods but if string are not in hand of any person then there is no direct connection but still person can see it and can access its properties this is weak reference.

RETAIN
Retain is count for strong reference that from how much objects variable connected as connection between objects get destroyed count of retain reduced by 1.

Leave a Comment