核心数据:有什么方法可以获取多个实体? [英] Core data: any way to fetch multiple entities?

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

问题描述

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

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.

举个例子,假设我有一个实体奶酪"和一个不相关的实体海盗".在我的应用程序的主屏幕上,用户应该能够创建Cheese"或Pirate"实例以添加到表格视图中.

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.

因此,使用我为 Cheese 和 Pirate 创建实体的核心数据编辑器……然而,NSFetchRequest 似乎只允许您一次检索一种类型的实体:

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?

谢谢.

推荐答案

您要做的是通过在模型中定义实体继承来完成,其中DisplayableObject"将是一个抽象类,定义为Cheese"和Pirate"的父类.

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".

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

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

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

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天全站免登陆