使用NSFetchedResultsController没有UITableView [英] Using NSFetchedResultsController Without UITableView

查看:101
本文介绍了使用NSFetchedResultsController没有UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 NSFetchedResultsController 纯粹用于数据管理,即不使用它来提供 UITableView

Is it wrong to use an NSFetchedResultsController purely for data management, i.e., without using it to feed a UITableView?

我在Core Data iPhone应用程序中有一对多的关系。每当该关系中的数据发生变化时,我需要执行一个计算,该计算需要对数据进行排序。在苹果的标准部门/员工示例中,这就像是确定某一部门的中间薪水。每当员工加入或移出该部门,或员工的工资发生变化时,都需要重新进行中值计算。

I have a to-many relationship in a Core Data iPhone app. Whenever data in that relationship changes, I need to perform a calculation which requires that data to be sorted. In Apple's standard Department/Employees example, this would be like determining the median salary in a given Department. Whenever an Employee is added to or removed from that Department, or an Employee's salary changes, the median calculation would need to be performed again.

保持数据排序,通知当它改变听起来像一个伟大的工作 NSFetchedResultsController 。唯一的问题是我不使用 UITableView 。换句话说,我不是在 UITableView 中显示排序的Employees。我只想要一个最新的排序的员工数组,所以我可以在幕后分析。 (当然,我不想写一堆重复大量 NSFetchedResultsController 的代码。)

Keeping data sorted and current and getting notifications when it changes sounds like a great job for NSFetchedResultsController. The only "problem" is that I'm not using a UITableView. In other words, I'm not displaying sorted Employees in a UITableView. I just want an up-to-date sorted array of Employees so I can analyze them behind the scenes. (And, of course, I don't want to write a bunch of code that duplicates much of NSFetchedResultsController.)

使用 NSFetchedResultsController 纯粹用于数据管理,即不使用它来提供 UITableView ?我没有在任何地方看到这个,并认为我可能会缺少一些东西。

Is it a bad idea to use an NSFetchedResultsController purely for data management, i.e., without using it to feed a UITableView? I haven't seen this done anywhere, and thought I might be missing something.

推荐答案

heavy。

通过 NSManagedObjectContextDidSaveNotification 可以节省更多的内存和CPU,那里。通知将在其 userInfo 中带有三个 NSArray 实例,然后可以使用简单的

It would be less memory and CPU to watch for saves via the NSManagedObjectContextDidSaveNotification and do the calculation there. The notification will come with three NSArray instances in its userInfo and you can then use a simple NSPredicate against those arrays to see if any employee that you care about has changed and respond.

这是<$ c $的一部分

This is part of what the NSFetchedResultsController does under the covers. However you would be avoiding the other portions of the NSFetchedResultsController that you don't care about or need.

NSFetchedResultsController 执行的处理不仅仅是监视已保存的对象。它处理三角洲,调用其委托等。我说它是以任何方式形状或形式是坏的。我所说的是,如果你只关心对象在你的关系发生了变化,你可以很容易地做到只是看通知。

NSFetchedResultsController does more processing than just watch for saved objects. It handles deltas, makes calls to its delegates, etc. I am not saying it is bad in any way shape or form. What I am saying is that if you only care about when objects have changed in your relationship, you can do it pretty easily by just watching for the notifications.

此外,没有理由保留任何内容,因为您已经持有部门实体,因此访问其关系。保持儿童对象以防万一是浪费记忆。让Core Data管理内存,这是使用它的原因的一部分。

In addition, there is no reason to retain anything since you are already holding onto the "Department" entity and therefore access its relationships. Holding onto the child objects "just in case" is a waste of memory. Let Core Data manage the memory, that is part of the reason for using it.

这篇关于使用NSFetchedResultsController没有UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆