问题描述
我正在开发一个大量使用Core Data的iPhone应用程序,主要用于其类似数据库的功能(例如,能够对获取请求设置排序顺序或谓词)。我展示了在各种UITableViewControllers中获取的所有数据。
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.
我想知道的是一个粗略的想法,好主意使用NSFetchedResultsController来处理请求。在Core Data文档中,它说SQLite存储器认为10,000个对象是一个相当小的数据集,但在NSFetchedResultsController的文档中,它提到每次在内存中保留数十个对象。
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.
我主要处理多达50个对象的数据集,每个对象可能有十几个NSNumber和NSString实例,以及下一组对象的一对多关系(即,存在对象A的二十个实例,每个实例具有与对象B的三十个(不同的)实例的集合的对多关系,其中...)。
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...).
这个场景是否适合使用NSFetchedResultsController,或者我可以使用一个简单的NSArray结果吗?我没有管理控制器的奇怪的问题(方便的方法来获取一个对象的UITableView索引路径,添加新的对象回上下文等)自己,我只是想知道内存使用情况每个方法。
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.
我应该提到的应用程序将主要针对iPhone 3G(不是S)和第一代iPod Touch用户,所以请保持这些有限的记忆平台。
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.
推荐答案
NSFetchedResultsController是一个令人难以置信的帮助类,用于连接Core Data和UITableViews。我的建议是使用它与每个具有核心数据支持的表视图。在每种情况下,我使用它,它大大减少了我编写的代码量。
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.
性能方面,它可以导致巨大的改进。而不是读取整个数据集,如果您使用-setFetchBatchSize:与NSFetchedResultsController中提供的NSFetchRequest,您可以执行批量获取,其中仅获取在表视图中显示的相关数据。数据不再显示也可以自动从内存中删除(或我的理解)。
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).
对于包含中等到大型数据集的表,这可以带来显着的性能提升。苹果工程师曾经说过,对于一个10,000项目的数据库,这可以将您的启动时间减少80%,内存使用量减少50%。
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%.
这篇关于Cocoa Touch:什么时候需要NSFetchedResultsController来管理Core Data fetch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!