Please consider subscribing to Mobile Developer Tips RSS feed or following us on Twitter
|
|
|
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!
Related posts:
- A Java Developer’s Guide to Threads on iPhone
- Launching Other Apps within an iPhone Application
- Launching the Browser from within an iPhone application
Leave Comment