Angel G. Olloqui
In my previous post I analyzed the fundamentals of ARC
. Today, I am going to write a quick post about some general advantages, problems and myths around it. I warn you that I will not enter in details, but it will be useful as an introduction to the next (and probably last) post of the series.
There are many advantages of using it, but I would summarize them on the following:
It Keeps it Simple Stupid (KISS)! Working with ARC makes your code easier to write, read and maintain. Therefore, your code will be less error prone and you could actually save some time writing (specially in dealloc methods).
Safer: Do you think your code is secure without ARC? Yes, you know how to retain your instance variables, but, let me ask you if you always retain your temporal variables. Probably not always, right? In the end, if it is a temporal variable there is little point on retaining a variable and releasing it a few lines later right? Well, then check out the last example exposed in my previous post! You can never be absolutely sure if an autoreleased returned value will be available the full variable life time, so you should retain always autoreleased variables to be 100% sure it will work. ARC variables by default are strong, which means that they will be retained, and the compiler never forgets :). Besides that, the introduction of weak references can also help a lot in solving problems with released pointed objects (usually delegates).
Less leaks: If you are a master of iOS development you probably have few memory leaks or even not at all. However, even if that is the case is common to work with different people who might not be as experienced as you are. The use of ARC ensures that there is no leak due to an incorrect use of retain/release. However, be careful with the memory retain cycles! they are still there.
Reduces autorelease pools: This one is pretty interesting because I thought that ARC would make heavy use of autoreleasing pools. However, that’s part of a false myth and we will take a look to it later. For now, all you need to know is that it actually reduces the amount of autoreleases in your code (if everything is ARC compatible though), which could make your apps to actually execute faster if your code produces heavy memory pools.