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

问题描述 投票:1回答:1

我在我的应用程序中使用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;
    }

谁能看到我做错了什么?

ios core-data predicate
1个回答
1
投票

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

[NSPredicate predicateWithFormat:@"rel.attr == %@", value]

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

© www.soinside.com 2019 - 2024. All rights reserved.