Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa Touch: When does an NSFetchedResultsController become necessary to manage a Core Data fetch?

I'm developing an iPhone application that makes heavy use of Core Data, primarily for its database-like features (such as the ability to set a sort order or predicate on fetch requests). I'm presenting all the data I fetch in various UITableViewControllers.

What I'd like to know is a rough idea of how many objects I can fetch before it becomes a good idea to use an NSFetchedResultsController to handle the request. In the Core Data docs, it says that SQLite stores consider "10,000 objects to be a fairly small data set," but in the documentation for NSFetchedResultsController it mentions keeping "tens of objects" in memory at a time.

I'm dealing primarily with data sets of up to fifty objects that each have maybe a dozen instances of NSNumber and NSString, as well as a one-to-many relationship for the next set of objects (i.e. there are twenty instances of object A, each of which have a to-many relationship to a set of thirty (distinct) instances of object B, each of which...).

Is this scenario a good fit for using an NSFetchedResultsController, or can I get away with a simple NSArray of results? I don't have an issue with managing the niceties of the controller (convenience methods to get an object for a UITableView index path, adding new objects back to the context, etc.) myself, I'm just wondering about the memory usage of each approach.

I should mention the app will be targeted primarily at iPhone 3G (not S) and first-gen iPod Touch users, so please keep the limited memory of these platforms in mind.

like image 974
Tim Avatar asked Aug 12 '09 00:08

Tim


1 Answers

NSFetchedResultsController is an incredibly handy helper class for interfacing Core Data with your UITableViews. My recommendation would be to use it with every table view that has a Core Data backing. In every case I've used it for, it significantly reduced the amount of code I had to write.

Performance-wise, it can lead to a huge improvement as well. Rather than fetching in your entire data set, if you use -setFetchBatchSize: with the NSFetchRequest that you feed into the NSFetchedResultsController, you can do batched fetching where only the relevant data being displayed in your table view is fetched. Data no longer on display can also be removed from memory automatically (or so is my understanding).

For tables with moderate to large data sets, this can lead to a significant performance win. Apple engineers have been quoted as saying that for a 10,000 item database, this can reduce your startup time by over 80% and your memory usage by 50%.

like image 166
Brad Larson Avatar answered Sep 22 '22 07:09

Brad Larson