NSPredicate with a !=?

前端 未结 2 925
滥情空心
滥情空心 2021-01-27 05:41

I have Core Data Entities Person and Boundary. They have a many-to-many relationship (each person can have many boundaries, and each boundary can have many persons).

I a

相关标签:
2条回答
  • 2021-01-27 06:38

    you need to ask first in Class Boundary if there is a person called Fred that has boundaries. It should be something like:

    NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Boundary"];
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"ANY persons.name != %@", person.name];
    

    That will fetch all the boundaries that Fred doesn't have.

    0 讨论(0)
  • 2021-01-27 06:39
    [NSPredicate predicateWithFormat:@"ANY persons != %@", fred]
    

    finds all objects that are related to any person other that Fred. What you want is

    [NSPredicate predicateWithFormat:@"NOT(ANY persons = %@)", fred]
    

    and this should return all objects that are not related to Fred.

    However, there seems to be a Core Data bug that "NOT ANY" or "NONE" do not work correctly in a predicate, compare NSPredicate Aggregate Operations with NONE. The workaround is to use a SUBQUERY:

    [NSPredicate predicateWithFormat:@"SUBQUERY(persons, $p, $p == %@).@count == 0", fred]
    
    0 讨论(0)
提交回复
热议问题