iOS 高效过滤 NSFetchedResultsController

发布于 2024-10-15 20:20:41 字数 346 浏览 1 评论 0原文

我正在尝试向 Core Data 支持的 UITableView 添加搜索支持,并开始遇到一些性能问题。我目前与两个 NSFetchedResultsController 关联(一个用于常规,一个用于搜索)。在我的 UISearchDisplayDelegate 方法中,我在每次调用后释放并重新创建我的 NSFetchedResultsController 。但是,这会导致打字时出现延迟!是否有更有效的方法来过滤 NSFetchedResultsController?我已将批量大小设置为大约 50 个项目,并且我的数据库有几千条记录(如果这有影响的话)。谢谢!

I'm attempting to add search support to a Core Data backed UITableView and started to hit some performance issues. I currently have associations to two NSFetchedResultsController (one for regular and one for searching). In my UISearchDisplayDelegate methods I release and recreate my NSFetchedResultsController after each call. However, this causes lag while typing! Is there a more efficient way to filter an NSFetchedResultsController? I've setup my batch size to around 50 items and my database has a few thousand records if that makes a difference. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混吃等死 2024-10-22 20:20:41

NSFetchedResultsController 文档 使其成为可能很清楚:

  • 如果有缓存,请将其删除。
  • 更新 fetchRequest 属性。请注意,您不能简单地修改现有的获取请求;您必须创建一个新的。
  • 调用-executeFetch:

The NSFetchedResultsController documentation makes it pretty clear:

  • If there is a cache, delete it.
  • Update the fetchRequest property. Note that you cannot simply modify the existing fetch request; you must create a new one.
  • Call -executeFetch:.
家住魔仙堡 2024-10-22 20:20:41

我使用以下代码来过滤 FetchedResultController 的结果

NSPredicate *pre = [NSPredicate predicateWithFormat:@"attribute CONTAINS [cd] %@", searchString];
NSArray *searchResults = [[self.fetchedResultsController fetchedObjects] filteredArrayUsingPredicate:pre]

希望有帮助!

I use the following code to filter the results of the FetchedResultController

NSPredicate *pre = [NSPredicate predicateWithFormat:@"attribute CONTAINS [cd] %@", searchString];
NSArray *searchResults = [[self.fetchedResultsController fetchedObjects] filteredArrayUsingPredicate:pre]

Hope that helps!

酒解孤独 2024-10-22 20:20:41

是的,您可以:

创建 NSFetchRequest 实例并每次更改其排序描述符:

 let shortDescriptor = NSSortDescriptor(key: key, ascending: ascending)
    request.sortDescriptors = [shortDescriptor]

    do {
        try fetchedResultViewController?.performFetch()
    } catch let error as NSError {
        print("Error in fetch \(error)")
    }

阅读苹果文档: https ://developer.apple.com/reference/coredata/nsfetchedresultscontroller

Yes you can:

Create the NSFetchRequest instance and change its sort descriptor each time:

 let shortDescriptor = NSSortDescriptor(key: key, ascending: ascending)
    request.sortDescriptors = [shortDescriptor]

    do {
        try fetchedResultViewController?.performFetch()
    } catch let error as NSError {
        print("Error in fetch \(error)")
    }

Read the apple document: https://developer.apple.com/reference/coredata/nsfetchedresultscontroller

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文