CoreData 和大表
我有一个 Cocoa Mac 应用程序,其中有一个搜索字段和一个通过 CoreData 绑定到 sqlite 表的集合视图。该表包含数十万条记录,其中文本字段(名称、地点等)按名称索引。我在搜索字段绑定中使用 BEGINSWITH 谓词来选择要在集合视图中显示的十几条记录。一切工作正常,但问题是 CoreData 在第一个查询请求时将整个表加载到内存中,然后才对显示的记录进行必要的过滤,这对用户来说意味着相当大的延迟。
有没有办法设置 CoreData 以便整个表不会加载到内存中?理想情况下,我只想为搜索字段中的每个查询从一系列按字母顺序排序的记录中获取前 100 项。
I have a Cocoa Mac application with a search field and a collection view bound to an sqlite table through CoreData. The table contains several hundred thousand records with text fields (name, place, ...) indexed by name. I'm using BEGINSWITH predicate in a search field binding to select a dozen of records for display in a collection view. Everything works fine, but the problem is that CoreData loads the whole table into memory at the first query request and only then does the necessary filtering of records for display which means considerable delay for the user.
Is there a way to set up CoreData so that the whole table does not load into memory? Ideally, I would like to fetch only the first 100 items from a range of alphabetically sorted records for every query in the search field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 iOS 上,这可以使用 NSFetchedResultsController 来实现。以下问题描述了 Mac 等效控制器(例如
NSArrayController
):NSFetchedResultsController Mac OSX Cocoa 等效项。编辑
根据我下面的评论,应与数组控制器一起创建
NSFetchRequest
。然后可以使用 fetchLimit 和 fetchOffset 进行配置,以确定返回多少个提取结果。On iOS, this would be achieved using a
NSFetchedResultsController
. The following question describes the Mac equivalent controllers (such asNSArrayController
): NSFetchedResultsController Mac OSX Cocoa equivalent.EDIT
As per my comment below, a
NSFetchRequest
should be created in conjunction with the array controller. This can then be configured withfetchLimit
andfetchOffset
to determine how many fetch results are returned.确保您将获取请求设置为仅作为错误获取。这样,只有那些属性被主动访问的对象才会被完全加载到内存中。
Make sure that you have the fetch request set to fetch as faults only. That way only the objects whose attributes who are actively accessed will be fully loaded into memory.