不同实体的多个 NSFetchedResultController?
我正在检查 iPhone Core Data 项目的默认 Xcode 模板。在返回获取的结果控制器的方法中,我看到了这一点:
- (NSFetchedResultsController *)fetchedResultsController {
...
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
...
return fetchedResultsController;
}
它似乎正在设置特定的实体名称。如果我有多个实体怎么办?我是否有 2 个 NSFetchedResultsController 实例,并有 2 个方法根据我使用的实体返回正确的控制器?
谢谢
I'm checking out the default Xcode template for an iPhone Core Data project. In the method that returns the fetched result controller I see this:
- (NSFetchedResultsController *)fetchedResultsController {
...
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
...
return fetchedResultsController;
}
It seems to be setting specific entity name. What If I have multiple entities? Would I have 2 NSFetchedResultsController instances and have 2 methods that return the correct controller depending on which entity I'm using?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于。
例如,如果您有一个 Person 实体和一个继承自 Person 的 Employee 实体,那么您可以对一个 Person 实体使用一个 NSFetchedResultsController 来获取 Person 和Employees。但是,如果您有类似 Fruit 实体和 Person 实体(并且 Person 不从 Fruit 继承,反之亦然),那么您不太可能使用 1 NSFetchedResultsController 来获取 Fruits 和 Person。
您是否需要 1 个或多个 NSFetchedResultsController 取决于您的实体继承层次结构。
It depends.
For example, if you have a Person entity and Employee entity that inherits from Person, then you can use one NSFetchedResultsController for a Person entity that would fetch both Persons and Employees. However, if you have something like Fruit entity and Person entity (and Person does not inherit from Fruit and vice versa) then it's unlikely that you can use 1 NSFetchedResultsController to get Fruits and Persons.
Whether or not you need 1 or more NSFetchedResultsController depends on your entity inheritance hierarchy.