具有一对多关系的 NSFetchedResultsController
这是我无法弄清楚如何使用核心数据的事情。我想使用 NSFetchedResultsControllerDelegate ,到目前为止我看到的常用代码很容易理解,但始终基于一个实体模型。因此,您想在表中显示所有“事件”,您对事件实体执行获取请求,然后就可以开始了。
问题是,我的模型是:
City (one-to-one) Company (one-to-many) Employees
我的表需要显示员工,但获取必须基于城市,以便检索公司,然后检索员工,对吧?我完全迷失了,我只是不知道如何进行获取。
因为如果我获取城市或公司并将员工放入 NSMutableSet 中,我不会失去所有自动 UITableViewController 同步吗?例如,如果我这样做,我将无法做类似的事情
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo =
[[_fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
This is something i just can't figure out working with Core Data. I want to work with an NSFetchedResultsControllerDelegate and the usual code i've seen so far is easy to understand but always based on a one entity model. So you want to show in your table all the "events", you do a fetch request on the Event entity and there, you are set to go.
The thing is, my model is:
City (one-to-one) Company (one-to-many) Employees
My table would need to show the employees, but the fetch would have to be based on the City, in order to retrieve the Company and then the Employees, right? I'm completely lost at this, i just don't see how to do the fetch.
Because if i fetch City or Company and i put Employees in an NSMutableSet, don't i loose all the authomatic UITableViewController syncing? For instance, if i do it like this, i will be unable to do something like
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo =
[[_fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的数据模型应该具有相互关系,以便当您获取任何特定对象时,您可以立即访问所有相关对象。
在您的情况下,具有互惠关系的数据模型将类似于:
因此,如果您有一个 Employee 对象,您可以通过以下方式找到带有 self.company 的公司和城市:
self.company.city
(在大多数情况下,实际的self
是不必要的,我将其用于说明目的。)如果您有一个Company
对象,您可以在self.employees
中找到员工,在self.city
中找到城市。如果您有一个City
对象,您将找到具有self.company.employees
的所有员工。关系实际上创建了核心数据核心的对象图。您可以使用提取来查找图表中的一组对象,然后从这些对象向外“遍历”关系以查找所有相关数据。正是互惠关系使得双向关系成为可能。
You data model should have reciprocal relationships such that when you fetch any particular object, you have immediate access to all related objects.
In your case, a data model with reciprocal relationships would look something like:
So, if you have an
Employee
object, you find the company withself.company
and the city byself.company.city
(in most case the actualself
is unnecessary and I use it for illustration purposes.) If you have aCompany
object, you find employees inself.employees
and the city inself.city
. If you have aCity
object you would find all the employees withself.company.employees
.Relationships are what actually create the object graph at the heart of Core Data. You use fetches to find one group of objects in the graph and then you "walk" the relationships outward from those objects to find all the related data. It is the reciprocal relationships that make it possible to go back and forth across the relationships in both directions.
迟到总比不到好。
使用谓词
从这个链接开始,http://blog.sallarp .com/iphone-core-data-uitableview-drill-down/
如果您打算创建一个泛型类并使其适用于不同的父子实体,请确保删除 NSFetchedResultsController 的缓存名称。
Better late than never.
Use a predicate
Go it from this link, http://blog.sallarp.com/iphone-core-data-uitableview-drill-down/
If you're planning to make a generic class and make this work for different parent-child entities, make sure to delete the cache name of you NSFetchedResultsController.
我认为您应该使用“城市”属性或“公司名称”属性或两者的过滤器来获取“公司”托管对象。如果您已在公司和员工之间设置了一对多关系,则您的公司托管对象应具有一个 NSSet 属性,该属性包含列表所需的所有员工对象。
I think you should be fetching the "Company" managed object with a filter on the City attribute or the Company Name attribute, or both. If you have set up the one-to-many relationship between Company and Employees, then your Company managed object should have a NSSet property that has all of the employee objects that you will need for your list.