Chris Lundie’s Blog

I’m a software developer from Canada.
You can view my portfolio at www.Lundie.ca.

Apr 26

Responsive UI on the iPhone and iPad

Today I released an update to my iPhone & iPad app LinkDew. I think it does a decent job of keeping the UI responsive while doing some fairly intensive background operations.

The Problem

LinkDew is a Twitter client that displays photos uploaded by the people you follow. It uses Core Data to save information about your friends, their tweets and photos that were linked in the tweets. In the iPhone simulator, running on the computer’s Core 2 Duo processor, everything is so fast, you don’t notice any delay. Running on a real iPhone 3G, it can actually take two or three seconds to parse the results from the Twitter API, extract all the photo links, insert the Core Data objects representing the links and statuses, and various other operations. If this were done on the main thread, the UI would be blocked and everything would freeze up for a few moments. This is not good.

The Solution

I moved all of the heavy lifting to a background thread. The tricky part is that a Core Data context (NSManagedObjectContext) cannot be shared between two threads. So, the background thread needs to make its own context. This is straightforward, but what happens when the importing operation is done, and you save the context? Well, the context you are using on the main thread will not know anything about the changes that were made on the other context. What to do?

Core Data was designed with this in mind! You just need to register for this notification:

NSManagedObjectContextDidSaveNotification

When the “background” context sends this notification, I just call mergeChangesFromContextDidSaveNotification: on the “main” context, and make sure to do so on the main thread. Then everything syncs up and the UI is never blocked.


  1. chrislundie posted this
Page 1 of 1