Master data, NSPredicate key and to-many

I have a Core Data model in which a Task object includes an optional to-many excludedOccurrences relationship. One of the properties of excludedOccurrences is the launch, which is an NSDate object. An ExcludedOccurrence object has an inverse binding relation to a single Task object.

To receive tasks for the specified day, I need to make sure that the specified day is not displayed as the start property of any ExcludedOccurrence object. Therefore, one of the sub-predicates that I am trying to use is

NSPredicate *occurrenceIsNotExcludedPredicate = [NSPredicate predicateWithFormat: @"(ALL excludedOccurrences.start != %@))", today]; 

where today is the NSDate object for today, including only the components of the day, month, and year. All properties for triggering excluded occurrences also include the components of the day, month, and year.

Although this should be good, at least for reading the documentation for Core Data and NSPredicate, I get the following error message:

Application termination due to an uncaught exception "NSInvalidArgumentException", reason: "Unsupported predicate

If I use an equivalent predicate

 NSPredicate *occurrenceIsNotExcludedPredicate = [NSPredicate predicateWithFormat: @"!(ANY excludedOccurrences.start == %@))", today]; 

an exception is not thrown, however, the code does not work as expected: an exception from this list that should not be excluded.

I'm also not sure how to test for case excludedOccurrences == nil: the following predicate

 NSPredicate *nilPredicate = [NSPredicate predicateWithFormat: @"(excludedOccurrences == nil)"]; 

raises an exception at runtime

to-many is not allowed here

However, since the excludedOccurrences relationship is optional, I also need to check if it is.

How can I handle this? Thank you in advance.

+42
iphone core-data nspredicate
Jul 22 '09 at 10:25
source share
4 answers

with the help of all of you, finally, I was able to determine the correct predicate for my scenario. It seems that the NSDate object is being processed as double, however double is never something like 3.7, it is always like 3.0. Therefore, the following predicate works correctly in my tests:

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

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

Testing empty relationships is mostly done using the @count aggregation operator, as suggested by some people at Apple.

Again, many thanks for your help. I still notice that the documentation is erroneous in several parts (especially where it says that EVERYTHING works fine, but instead it doesn't work at all).

+28
Jul 29 '09 at 15:29
source share

To check for an empty relation, you must compare the number of to-many keys with zero.

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

As for your sub-edits, keep in mind that you can only have one of the modifiers ALL or ANY in your final predicate, although you can use this modifier several 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)

+108
Jul 28 '09 at 17:21
source share

So, to check for a non-empty relation, this really works:

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

The decision given by Ashley Clark breaks down because I give me the "key for many"

+8
Jan 11 '10 at 4:42
source share

And in swift 2, something like:

 request.predicate = NSPredicate(format: " relationship.@count != 0") 
+3
Nov 27 '15 at 7:46
source share



All Articles