I am trying to use NSPredicate to filter a Core Data fetch request. The predicate string looks like this:
"stringProperty = %@ && dateProperty > %@ && oneToOneRelationship != nil"
This is isn't returning any objects. However, if I omit the last clause it does. If I change the last clause to be "oneToOneRelationship = nil" then it does. Of course, these last two cases don't return the data I really want. :)
Is there a proper way to search for one-to-one relationships where the relationship exists and isn't nil? It seems like that predicate should be working but it isn't. This is on the current Yosemite beta using Xcode 6.1 beta.
Anyone got any ideas?
Use oneToOneRelationship.@count != 0. The following code should work:
Swift:
let predicate = NSPredicate(format: "stringProperty == %@ && dateProperty > %@ && oneToOneRelationship.@count != 0", argumentArray:[stringProperty, dateProperty])
fetchRequest.predicate = predicate
Objective-C:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"stringProperty == %@ && dateProperty > %@ && oneToOneRelationship.@count != 0", stringProperty, dateProperty];
[fetchRequest setPredicate:predicate];
Actually, the "!= nil" test is working just fine. It turns out that although I thought the one-to-one relationship was being set, in fact, it wasn't. So, the predicate was working properly. It was, of course, my code that was in error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With