Monday, December 14, 2009

Last minute optimization and lazy fetching

This post is about how a tiny method saved me a lot of additional work. Maybe you know that I am the developer of Ebbinghaus. Ebbinghaus is a flash card application. It heavily uses Core Data. Like every good developer I didn't really care about performance issues too early. In fact I did test the performance of the whole application when it was done. I remembered the following quote.
Premature optimization is the root of all evil. (Donald Knuth)
The performance tests showed that Ebbinghaus had only one performance related problem. Let me describe the problem: Ebbinghaus lets the user create decks. Every deck can contain cards. The user is also able to create smart decks. They work like smart playlists you know from iTunes.

Filtering a lot of cards (+ 10.000) just was too slow. Scrolling the table view displaying the cards was not smooth at all. I asked myself how to solve this problem without having to write a lot of caching code myself. The solution was extremely easy. Maybe you already know: NSObjectController (and thus NSArrayController too)  has a method called -setUsesLazyFetching:. When setting -usesLazyFetching to YES the controller will try everything it can to improve performance by lazy fetching data. There is even a check box for this property so that you can set it directly in Interface Builder.

Checking this check box did solve the performance problem. I am still thrilled about this. Imagine what it kind of ugly work I had to do then implementing that for myself. Of course lazy fetching does only work  in combination with Core Data. So be a smart developer and use Core Data whenever you can.

No comments:

Post a Comment