在运行时过滤 NSFetchedResultsController 结果

发布于 2024-10-30 21:55:38 字数 333 浏览 1 评论 0原文

使用 NSFetchedResultsController 实现 Core Data 对象的运行时过滤的最佳方法是什么?

例如,我希望能够显示 RecordStore 实体中的所有记录实体,而且还可以针对某些预定义的条件过滤 RecordStore 中的所有记录,例如(ANY recordStore.records.count > 0)。

我读到,在创建 NSFetchedResultsController 的谓词后更改它是不好的。那么我应该将获取的结果存储在 NSArray 中,我可以过滤并将其用作 UITableView 的数据源,还是应该创建多个 NSFetchedResultsController?

What's the best way to implement runtime filtering of Core Data objects using NSFetchedResultsController?

For example, I want to be able to display all Record-entities in a RecordStore-entity, but also filter all Records in a RecordStore for some predefined critera, eg (ANY recordStore.records.count > 0).

I read that changing an NSFetchedResultsController's predicate after it has been created is bad. So should I store the fetched results in an NSArray that I can filter and use that as the UITableView's datasource, or should I create multiple NSFetchedResultsControllers?

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

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

发布评论

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

评论(2

素染倾城色 2024-11-06 21:55:38

当需要更新时,可以重新获取数据。如果数据本身发生了变化,那么您只需再次调用 fetch 即可。如果您的标准发生变化,则在 NSFetchedResultsController 上设置谓词,然后调用 fetch。

You can re-fetch the data when you need to update. If the data itself changed, then you can just call fetch again. If your criteria changed, then set the predicate on your NSFetchedResultsController, and call fetch.

缺⑴份安定 2024-11-06 21:55:38

创建 NSFetchRequest 实例,每次更新请求并执行 fetch。如果您使用的是删除缓存:

    let shortDescriptor = NSSortDescriptor(key: key, ascending: ascending)


    fetchedResultViewController.fetchRequest.sortDescriptors = [shortDescriptor]

    NSFetchedResultsController<NSFetchRequestResult>.deleteCache(withName: fetchedResultViewController.cacheName)

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

请阅读苹果文档: https://developer.apple.com/参考/coredata/nsfetchedresultscontroller

Create the NSFetchRequest instance, update request each time and perform fetch. Delete cache if you are using:

    let shortDescriptor = NSSortDescriptor(key: key, ascending: ascending)


    fetchedResultViewController.fetchRequest.sortDescriptors = [shortDescriptor]

    NSFetchedResultsController<NSFetchRequestResult>.deleteCache(withName: fetchedResultViewController.cacheName)

    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 和您的相关数据。
原文