【问题标题】:Core Data: Multiple conditions inside relational aggregate operations核心数据:关系聚合操作中的多个条件
【发布时间】:2014-06-02 03:21:26
【问题描述】:

我有一个 Core Data 使用的 SQLite 表,其中包含以下元素:

姓名:约翰
姓氏:Foobar
年龄:23​​

姓名:鲍勃
姓氏:巴兹
年龄:37

姓名:彼得
姓氏:Fooqux
年龄:32

姓名:约翰
姓氏:酒吧
年龄:29

这些都是来自另一个对象“公司”的一对多关系。我需要查询数据库并检索所有名为“John”但姓氏不包含“Foo”的员工的 Company 对象。

我确实做了以下谓词:

[NSPredicate predicateWithFormat:@"ANY employee.name = 'John'"];

如何仅按 John 的姓氏中没有“Foo”的公司进行过滤?

【问题讨论】:

  • 这可能与THIS 重复。 THIS 也可能有帮助

标签: ios core-data


【解决方案1】:

假设您将您的一对多关系命名为“员工”,这里有一个示例,它将获取所有符合您要求的标准的公司。

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    //Fetch all companies 
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@“Company” inManagedObjectContext:context];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SUBQUERY(employees, $x, $x.name = ‘John’ AND NOT $x.lastName = ‘Foo’).@count > 0)"];

    //Set the predicate
    [request setPredicate:predicate];

    NSError *error;

    NSArray *filteredCompanies = [context executeFetchRequest:request error:&error];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 2014-03-20
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    相关资源
    最近更新 更多