尝试使用Entity关系对象的属性作为Core Data中的搜索条件来检索实体

时间:2013-11-23 21:44:21

标签: ios core-data predicate

我在我的应用程序中使用Core Data,我正在尝试检索其关系对象属性符合我的条件的实体。不幸的是我被困在这里,因为我传递了关系对象的id,但是我收到一个错误,说我传递的标准没有被识别为我正在查询的实体的属性。

这是事实,因为我的标准实际上是关系对象的属性,而不是我查询自身的实体的属性。我如何实现这一目标?

这是我的代码:

    // Create fetch request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:myEntityName inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    // Create predicate
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"relationshipObjectId == %@", relationshipObjectId];//This is where I am having trouble
    [fetchRequest setPredicate:pred];

    NSArray *items = [context executeFetchRequest:fetchRequest error:&error];
    if ([items count]>0) {
        return items[0];
    } else {
        return nil;
    }

任何人都可以看到我做错了吗?

1 个答案:

答案 0 :(得分:1)

要查找属性与特定值匹配的所有相关对象,您可以 使用像

这样的谓词
[NSPredicate predicateWithFormat:@"rel.attr == %@", value]

其中“rel”是关系的名称,“attr”是名称 应与该值匹配的属性。