atomic nonatomic retain assign copy

Abhishek Kumar
3 min readMay 7, 2019

--

Atomic and NonAtomic has been one of the most debated and wrongly documented by some of the developers in their blogs. Let’s understand once for all.

Atomicity:

Atomic (default) : Atomic is the default: if you don’t type anything, your property is atomic.An atomic property is guaranteed that if you try to read from it, you will get back a valid value. It does not make any guarantees about what that value might be, but you will get back good data, not just junk memory. What this allows you to do is if you have multiple threads or multiple processes pointing at a single variable, one thread can read and another thread can write.

If they hit at the same time, the reader thread is guaranteed to get one of the two values: either before the change or after the change.

What atomic does not give you is any sort of guarantee about which of those values you might get. Atomic is really commonly confused with being thread-safe, and that is not correct. You need to guarantee your thread safety other ways. However, atomic will guarantee that if you try to read, you get back some kind of value.

NonAtomic : On the flip side, non-atomic, as you can probably guess, just means, “don’t do that atomic stuff.” What you lose is that guarantee that you always get back something. If you try to read in the middle of a write, you could get back garbage data. But, on the other hand, you go a little bit faster. Because atomic properties have to do some magic to guarantee that you will get back a value, they are a bit slower. If it is a property that you are accessing a lot, you may want to drop down to nonatomic to make sure that you are not incurring that speed penalty.

Access :

The second category of property attributes you can have are ones that modify access. There are also two in this class: readonly and readwrite

readonly : readonly makes it so this property is read-only. There is no setter for it at all.

readwrite(default) : readwrite, allows both read and write. That is the default; again, if you do not write anything, you have a readwrite property.

Storage:

strong (default) :

The default is called strong. Strong just means you have a reference to an object and you will keep that object alive. As long as you hold that reference to the object in that property, that object will not be deallocated and released back into memory.

weak :

Strong’s analog is weak. Weak gives you a reference so you can still “talk” to that object, but you are not keeping it alive. If everyone else’s strong references go away, then that object is released. The nice thing about weak references is that they will automatically nil references that go away. If you have a reference to an object and all of the strong references to it go away and it gets deallocated, your weak reference does not point to some junk memory, which can give you crashes. It will just automatically nil itself out.

assign :

assign is the keyword used for primitives. It is pretty easy to understand: If you do not have an object, you cannot use strong, because strong tells the compiler how to work with pointers. But if you have a primitive (i.e. int, float, bool, or something without the little asterisk after the type), then you use assign, and that makes it work with primitives.

retain :

increases the retain count of an object by 1. Takes ownership of an object.

copy : Makes a copy of an object, and returns it with retain count of 1. If you copy an object, you own the copy. This applies to any method that contains the word copy where “copy” refers to the object being returned.

--

--