NSPredicate won't create where clause

I've tried all three of these statements, and all three give this error:


Unable to generate where clause for predicate (matches2.bracket == <Brackets.Bracket: 0x7f9379c63f90> (entity: Bracket; id: 0xd000000000380000 <x-coredata:/

created = "2015-09-16 01:49:46 +0000";

locations = "<relationship fault: 0x7f9379956f50 'locations'>";

matches = "<relationship fault: 0x7f93799570a0 'matches'>";

playoff = "0xd0000000034c0004 <x-coredata:/

secondPlayoff = nil;

})) (unknown problem)


These are the three predicates I tried, all with the exact same result:



request.predicate = NSPredicate(format: "matches2.bracket = %@", self)
request.predicate = NSPredicate(format: "%K.bracket = %@", "matches2", self)
request.predicate = NSPredicate(format: "%K = %@", "matches2.bracket", self)
Answered by TommieC in 56338022

What the error is stating is that the Core Data object you are trying to query does not have an attribute called bracket. I can see that you are attempting to access a faulted attribute called matches which probably has some additional bracket objects linked within but you will not be successful with this approach.


Core Data just works a bit differently.


If you want to test equality for a specific object then you might look at setting up the hash and isEqual functions for the custom object. However, even if you make that work you will find that performance degrades way beyond what you are expecting. You could also look at using the managedobjectid for the record and then querying those for a match. Again, your mileage may vary depending on how you plan to query this ~ equality queries are very expensive overall and with a lot of objects the performance will just shut down.


I'd suggest you try creating a separate object that holds a single attribute value which can be search using a BEGINSWITH predicate. The separate core data object (Tokens) might hold a single relationship back to Brackets. When you run the BEGINSWITH predicate against a single object you can fault in the related bracket objects and your performance will be excellent. Although not easy to implement, this is the correct approach you will need to take with Core Data.


Unfortunately, I am in the midst of building this myself and have found no sample code to assist. A demo of this was covered in Session 211 of the 2013 WWDC (). Please let me know if this helps you resolve the question and feel free to update with your progress as you build a solution.

I am unfamiliar with ".bracket" or why you would want a predicate that checks to see if something is equal to 'self' (which is the class that contains this code). Are you sure that is what you want?

Accepted Answer

What the error is stating is that the Core Data object you are trying to query does not have an attribute called bracket. I can see that you are attempting to access a faulted attribute called matches which probably has some additional bracket objects linked within but you will not be successful with this approach.


Core Data just works a bit differently.


If you want to test equality for a specific object then you might look at setting up the hash and isEqual functions for the custom object. However, even if you make that work you will find that performance degrades way beyond what you are expecting. You could also look at using the managedobjectid for the record and then querying those for a match. Again, your mileage may vary depending on how you plan to query this ~ equality queries are very expensive overall and with a lot of objects the performance will just shut down.


I'd suggest you try creating a separate object that holds a single attribute value which can be search using a BEGINSWITH predicate. The separate core data object (Tokens) might hold a single relationship back to Brackets. When you run the BEGINSWITH predicate against a single object you can fault in the related bracket objects and your performance will be excellent. Although not easy to implement, this is the correct approach you will need to take with Core Data.


Unfortunately, I am in the midst of building this myself and have found no sample code to assist. A demo of this was covered in Session 211 of the 2013 WWDC (). Please let me know if this helps you resolve the question and feel free to update with your progress as you build a solution.

So you *can* query like this, through the object, but you're right, .bracket doesn't exist on the object being queried (like it used to) and thus the error. Thanks.

NSPredicate won't create where clause
 
 
Q