Core Data, NSPredicate and to-many key

后端 未结 4 1226
别跟我提以往
别跟我提以往 2020-12-08 06:45

I have a Core Data model in which a Task entity includes an optional to-many relationship excludedOccurrences. One of the properties of excludedOccurrences is start, which i

相关标签:
4条回答
  • 2020-12-08 07:06

    with the help of you all, I finally managed to determine the correct predicate for my scenario. It looks like that an NSDate object is handled as a double, however, the double is never something like 3.7, it is always like 3.0 Therefore, the following predicate correctly works in my tests:

    NSPredicate *occurrenceIsNotExcludedPredicate = [NSPredicate predicateWithFormat: @"(excludedOccurrences.@count == 0 || (excludedOccurrences.@count > 0 && NONE excludedOccurrences.start == %@))",thisDate];
    

    where thisDate is a NSDate object containing only the day, month and year components (as in the case of the start property of the ExcludedOccurrence entity.

    Testing for an empty relationship is basically done using the @count aggregate operator, as suggested by some folks at Apple.

    Again, thankyou very much for your help. I still observe that the documentation is flawed in several parts (especially where it says that ALL works fine while, instead, it does not work at all).

    0 讨论(0)
  • 2020-12-08 07:16

    So, to test for a non-empty relationship, this actually works:

    [NSPredicate predicateWithFormat:@"relationship.@count != 0"]
    

    The solution given by Ashley Clark crashes for me giving "to-many key not allowed here"

    0 讨论(0)
  • 2020-12-08 07:23

    To test for an empty relationship you should compare the count of the to-many key to zero.

    [NSPredicate predicateWithFormat:@"excludedOccurrences.@count == 0"];
    

    As for your subpredicates, be aware that you can only have one of either the ALL or ANY modifiers in your final predicate, although you can use that modifier multiple times throughout the predicate.

    Not OK: ANY foo.bar = 1 AND ALL foo.baz = 2
    OK: ANY foo.bar = 1 AND !(ANY foo.baz != 2)

    0 讨论(0)
  • 2020-12-08 07:24

    And in swift 2, something like:

    request.predicate = NSPredicate(format: " relationship.@count != 0")
    
    0 讨论(0)
提交回复
热议问题