How to “unfreeze” your iPhone Application

Have you ever found yourself stumped because your application locks up but doesn’t seem to crash or in any other way indicate that there is an error? If you have, then you are likely the victim of a common problem that can occur when you attempt to make certain changes to the UI outside of the main thread. Put more technically … you are doing something that is not “Thread Safe”.

To avoid “freezing” your application, ensure that any logic that is changing your UI is performed by the Main thread. One approach to this would be to leverage the NSNotificationCenter but sometimes that is overkill. A much simpler approach is to use one of the following performSelectorOnMainThread methods on NSObject:

1
2
performSelectorOnMainThread:withObject:waitUntilDone:
performSelectorOnMainThread:withObject:waitUntilDone:modes:

Nine-times-out-of-ten this is the magic that will “thaw” your application and get it humming along again!