简体   繁体   English

使用核心数据的最有效方法是什么?

[英]What is the most efficient way to use Core Data?

I'm developing an iPad application using Core Data, and was hoping someone could clarify something about Core Data. 我正在使用Core Data开发iPad应用程序,希望有人可以澄清有关Core Data的内容。

Right now, I populate my table by making a fetch request for all of my data in viewDidLoad . 现在,我通过对viewDidLoad所有数据进行提取请求来填充表。 I'd rather make individual fetch requests in my tableView:cellForRowAtIndexPath: . 我宁愿在tableView:cellForRowAtIndexPath:提出单个获取请求。 Can anyone tell me which is more efficient, and why? 谁能告诉我哪个更有效,为什么? In other words, is it much less efficient to make lots of small requests as opposed to one big request? 换句话说,发出大量小请求相对于发出一个大请求是否效率低得多?

Your fetch request has a property called fetchBatchSize that tells your fetch request only to fetch data in batches. 您的提取请求具有一个名为fetchBatchSize的属性,该属性告诉您的提取请求仅批量提取数据。 That means for a batch size of 20, only the first 20 items are loaded into memory. 这意味着对于20的批量大小,只有前20个项目被加载到内存中。 When the user scrolls past 20, another 20 items are loaded automatically, and so on. 当用户滚动到20时,将自动加载另外20个项目,依此类推。

Additionally, Apple ships an NSFetchedResultsController class that is designed for managing presentation and editing of your data (it works best with table views, but can work with other kinds of view controllers, of course). 此外,Apple附带了一个NSFetchedResultsController类,该类设计用于管理数据的表示和编辑(它最适用于表视图,但是当然可以与其他类型的视图控制器一起使用)。 It handles a lot of other optimization under the hood for you (things like batched fetching, memory management, etc), and as such offers much better performance than were you to create, initiate and manage your fetch requests yourself. 它为您处理了许多其他优化(如批量提取,内存管理等),因此,与您自己创建,发起和管理提取请求相比,它提供了更好的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM