限制 NSFetchedResultsController 结果,并获得更多
大家好,
我目前有一个 NSFetchedResultsController 设置来返回核心数据数据库中表中的所有行。然后这会填满我的 UITableView。问题是,随着行数的增加,这种情况很快就会失控。
如何将初始查询限制为 20 个结果,然后在某处添加一个按钮以从我们停止的地方“获取更多”?
感谢您一如既往的指导
HI All,
I currently have an NSFetchedResultsController setup to return all rows in a table in my core data database. This then fills up my UITableView. The trouble is this will quickly get out of hand as the rows grow in number.
How can I limit the initial query to say 20 results, then add a button somewhere to "Get More" from where we left off?
Thanks for any guidance as always
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是通过 NSFetchRequest 的 -setFetchLimit: 和 -setFetchOffSet。
如果我没记错的话,NSFetchedResultsController 的缺点是在创建 NSFetchedResultsController 实例后无法修改获取请求。我相信这意味着每次更改要检索/显示的范围时,您都必须创建一个新的实例(带有新的获取请求的实例)。
如果您认为不应出现这种情况,请通过 bugreporter.apple.com 向 Apple 提交增强请求。
This is controlled with NSFetchRequest's -setFetchLimit: and -setFetchOffSet.
If I recall correctly, the drawback with NSFetchedResultsController is that you can't modify the fetch request after you create your NSFetchedResultsController instance. I believe this means you'll have to create a new one (instance w/new fetch request) each time you change the range you want to retrieve/display.
File an enhancement request with Apple at bugreporter.apple.com if you feel this shouldn't be the case.
要动态更改限制数量,您只需:
访问 NSFetchedResultsController 实例的 fetchRequest,更改限制,删除旧缓存(如果有)并执行新的提取。
代码:
To change the limit number on the fly you simply need to:
Access the fetchRequest of your NSFetchedResultsController instance, change the limit, delete the old cache if there is any and perform a new fetch.
Code:
fetchBatchSize 仅影响一次获取的对象数量。它不会限制内存中并发对象的数量,因此仍然有可能耗尽内存。可以通过batchSize、fetchLimit和offset的组合来限制并发对象总数,但它需要删除缓存或为每个“页面”存储单独的缓存,这对我来说似乎不理想。
另一种更巧妙的解决方法是重新创建 NSFetchedResultsController,如果可能的话,旧控制器的结果将出现错误,您可以从头开始。确实很粗糙,但它避免了删除缓存。
fetchBatchSize only affects how many objects are fetched at a time. It will not limit number of objects in-memory concurrently so it is still possible to run out of memory. It is possible to limit the total concurrent objects with a combination of batchSize, fetchLimit, and offset but it requires deleting the cache or storing separate caches per "page", which seems un-ideal to me.
Another more hacky method to get around it is to re-create the NSFetchedResultsController, the results from the old controller will be faulted if possible, and you can start with a clean slate. Really crude, but it avoids deleting the cache.
我相信,不要设置 -setFetchLimit 并限制您的 NSFetchRequest (对于新行,您必须创建新的请求),而是设置 -fetchBatchSize 来仅控制将有多少行加载到内存中。假设,如果每个视图显示 10 个单元格,请将批量大小设置为两倍左右。当您滚动视图时,控制器会自动将新设置加载到内存中。
I believe that instead of setting -setFetchLimit and limiting your NSFetchRequest (for new rows you have to create a new reqeust), set -fetchBatchSize to only control how many rows will be loaded into memory. Say, If you show 10 cells per view, set your batch size to double or so. As you scroll your view, the controller will automatically load new set into memory.