iPhone app development is one of the most exciting and interesting work sector these days.
The very initial iPhone app developers were mostly veteran Mac OS application developers, but as time progressed and the app store started climbing the peak, more and more software developers and even non-developers started getting into the field.

Being a non-Objective C developer in the iPhone Arena isn’t simple in the beginning. For this simple reason, I am starting this series where I will put in issues that we might get into in the beginning.

In this post, I am presenting the very first issue, which many of us might run into:

When we refer the documentation of iPhone development, we assume that viewWillAppear method is called before the view is loaded into the memory and viewDidLoad method is called after the view has been allocated space in the memory. viewDidLoad as per documentation may be used for initialization steps for the views that have been loaded from the nib, similarly viewWillAppear method may be used for performing customization on the view like: status bar, setting orientation, navigation bar etc.

Another thing worth mentioning is, viewDidLoad as per theory would be called just once, i.e. when it is loaded, while viewWillAppear is called every time the view is presented.

Now, most of the iPhone apps are navigation based, and thus, we would have many views on stack most of time and would keep traversing between them as per the needs.

So, let’s say, I need two type of initializations to be done for one particular view, one of them is setting an array as null, which I would probably fill in as the app goes on, and the second one is setting the toolbar hidden.

You might think that it would be a good idea to have both the initializations done in viewDidLoad. But thats not true.

First of all, setting the toolbar hidden in the viewDidLoad will not work because the latter views might unhide it, and when get pushed back to this view, the viewDidLoad wouldn’t be called and thus, it will show up. Hence, the best solution is to have it in viewWillAppear, which will set it hidden every time this view is visible.

Secondly, initializing the array in viewDidLoad would make total sense as long as we do ignore the limited memory allocated to an app on an iPhone. To maintain the iPhone experience, the iPhone’s OS limits the app to a small amount of memory, which is somewhere around 25MB (this is not documented officially, but this is what I have experienced during development) on the non 3GS versions of the iPhone.

As soon as this limit is about to be reached, the OS would try to reload the navigation stack first and then even that doesn’t help closing the app. When the OS sees that the memory limit is about to be reached, it calls the didReceiveMemoryWarning method which in turn tries to reload all the view on navigation stack, and thus calling the viewDidLoad of the views again, which in turn would cause our array to be re-initialized.

Thus, to play safe, we should avoid initializing critical objects in viewDidLoad.

There can be many alternatives to avoid this problem, one might use the init method, but it again has some issues, which I might bring up in one of the following posts and the other one could be using a singleton object of the delegate and using a bool value in delegate class to check if the object needs to be initialized or not.

Even though the singleton bool value might not sound like the best solution, but it works in all the cases, and thus I prefer implementing that way. I would hope that this article would help somebody getting started with iPhone development. I would be more than happy to answer your questions in the comments section.

Sphere: Related Content

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.