The root of all bugs in iphone development?

by Martin Westin in ,


Catchy title, sure. I don't write that as a universal truth but as my experience from developing my first iPhone app, Extraction.

So what is it?

In a word (or two): Memory Warnings

Every single bug that was in my app when it shipped was related to memory warnings in some way. Had I tested memory warnings in the simulator more thoroughly they might never have bothered my users. My simple advice to other noob iphone developers is:

Simulate memory warnings in the simulator over and over in every conceivable view and state your app can be in.

Example #1. Lost data

Symptoms Rarely, but sometimes, my app would loose the most recent data while going back and forth between my main and my detail view. Really odd? I had many wild ideas of why it would suddenly loose the most recent "item".

The cause My app experienced occasional memory warnings (it takes photos which can for a brief time take up a lot of memory), sometimes causing the main view to unload and then reload when I returned to it. Normal behavior, but something I did not think much about. I had followed some tutorial or other (no finger-pointing here) and loaded my table of data in "viewDidLoad". Naturally when the view reloaded it also reloaded the data from disk and "lost" any recent data.

Example #2. The Disappearing GUI

Symptoms Rarely, but sometimes, the entire detail view would just "go blank" when the keyboard appeared to edit a text area. The navbar was still there but the rest of the screen above the keyboard was just a nice background color.

The cause Memory warnings again. ViewDidLoad was to blame again. This time the view registered itself to be notified about appearing keyboards and when the view reloaded after a memory warning it registered again and thus received notifications back-to-back. This caused my smooth scrolling to position the text area to happen twice and push everything off screen.

The moral of the story

ViewDidLoad can happen many times while your app is running. This is not too clear in most beginner tutorials.

Anything you do in a DidLoad or WillAppear you should "undo" (or do the reverse of) in the corresponding UnLoad or WillDisappear. For example:

Anything in the OS you register for, you really should resign from too.

This is all noob stuff but I think others may be scratching their heads over similar bugs. I hope it is not just me :)