Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

iphone - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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%.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...