核心数据:以任何方式获取多个实体? [英] Core data: any way to fetch multiple entities?

查看:89
本文介绍了核心数据:以任何方式获取多个实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Core Data,作为一个学习练习,我正在构建一个应用程序,我需要在单个表视图中显示不同类型的对象。



例如,我有一个实体为Cheese,一个不相关的实体为Pirate。在我的应用程序的主屏幕上,用户应该能够创建要添加到表视图中的Cheese或Pirate实例。



使用核心数据编辑器我已经为奶酪和海盗...创建实体,但是,NSFetchRequest似乎只允许您一次检索一种类型的实体:

  NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription * entity = [NSEntityDescription entityForName:@CheeseinManagedObjectContext:_context];
[fetchRequest setEntity:entity];

有没有办法执行检索所有Cheese和Pirate对象的抓取? / p>

感谢。

解决方案

通过在模型中定义实体继承,
其中DisplayableObject将是定义为Cheese和Pirate的父类的抽象类。



然后,您可以对DisplayableObject实体执行获取请求,它将检索这两个实体的对象。



查看苹果文档中的此文章: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/KeyConcepts.html


I'm just getting started with Core Data, and as a learning exercise I'm building an app where I need to display different types of objects in a single table view.

As an example, say I have an entity for "Cheese" and an unrelated entity for "Pirate". On the main screen of my app, the user should be able to create either a "Cheese" or a "Pirate" instance to add to the table view.

So, using the core data editor I've created entities for Cheese and Pirate... however, an NSFetchRequest seems to only allow you to retrieve one type of entity at a time with:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Cheese" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];

Is there any way to perform a fetch that retrieves all "Cheese" and "Pirate" objects?

Thanks.

解决方案

What you're trying to do is accomplished by defining entity inheritance in your model, where "DisplayableObject" would be an abstract class defined as the parent of "Cheese" and "Pirate".

Then you can perform a fetch request on the "DisplayableObject" entity and it will retrieve both entities' objects.

Take a look into this article in the apple documentation: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/KeyConcepts.html

这篇关于核心数据:以任何方式获取多个实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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