如何通过关系执行Core Data查询? [英] How to do Core Data queries through a relationship?

查看:125
本文介绍了如何通过关系执行Core Data查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在搞核心数据,我相信我缺少一些明显的东西,因为我找不到一个例子,在一切类似我想做的。

I'm messing around with Core Data, and I am sure I am missing something obvious, because I cannot find an example that at all resembles what I am trying to do.

假设我正在玩一个DVD数据库。我有两个实体。 A电影(标题,年份,评分和与Actor的关系)和Actor(姓名,性别,图片)。

Let's say I'm playing around with a DVD database. I have two entities. A Movie (title, year, rating, and a relationship to Actor) and Actor (name, sex, picture).

获得所有电影很容易。它只是:

Getting all the Movies is easy. It's just:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Winery"
inManagedObjectContext:self.managedObjectContext];

在标题中使用Kill获取所有电影很容易,我只是添加一个NSPredicate: / p>

Getting all the Movies with "Kill" in the title is easy, I just add a NSPredicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"name LIKE[c] "*\"Kill\"*""];

但是Core Data似乎抽象出托管对象的id字段...所以如何查询对象的属性(或:关系的查询)?

But Core Data seems to abstract out the id fields for the managed objects... so how to I query against an attribute that is an object (or: query against a relationship)?

换句话说,假设我已经有Actor对象,我关心([例如,对象ID 1 - 'Chuck Norris'],什么是给我所有电影主演的[对象id 1 - '查克·诺里斯']的谓词格式?

In other words, assuming that I already have the Actor object I am concerned with ([Object id 1 - 'Chuck Norris'] for instance), what is the Predicate format for "Give me all movies starring [Object id 1 - 'Chuck Norris']"?

推荐答案

假设Actor和Movie实体之间存在一对多的反向关系,你只需要获取Chuck Norris的实体,就可以得到任何特定的实体,然后访问附加到Actor实体上的关系的Movie实体数组。

Assuming that there is a one-to-many inverse relationship between the Actor and the Movie entities, you can just get the entity for Chuck Norris the same way you would get any specific entity, and then access the array of Movie entities attached to the relationship on the Actor entity.

// Obviously you should do proper error checking here... but for this example
// we'll assume that everything actually exists in the database and returns
// exactly what we expect.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Actor" inManagedObjectContext:self.managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE[c] 'Chuck Norris'"];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setPredicate:predicate];

// You need to have imported the interface for your actor entity somewhere
// before here...
NSError *error = nil;
YourActorObject *chuck = (YourActorObject*) [[self.managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0];

// Now just get the set as defined on your actor entity...
NSSet *moviesWithChuck = chuck.movies;

注意,这个例子显然假设10.5使用属性,但是你可以在10.4使用访问器方法。

As a note, this example obviously assumes 10.5 using properties, but you can do the same thing in 10.4 using accessor methods.

这篇关于如何通过关系执行Core Data查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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