Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MacOS: NSPredicate find where "one to one" relationship is not nil

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?

like image 458
ac4lt Avatar asked Oct 20 '25 03:10

ac4lt


2 Answers

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];
like image 154
Imanou Petit Avatar answered Oct 24 '25 19:10

Imanou Petit


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.

like image 44
ac4lt Avatar answered Oct 24 '25 20:10

ac4lt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!