I have a relation in Core Data like this:
A -> B
and the following tables:
A
----
id
path
ref_foreignkey_B
B
----
id
name
The problem is that the foreign key is set as B object (inherits from NSObject).
Then I am trying to fetch all entries from A where a reference to a certain B object is set. I know the object B but cannot figure out how to set the NSPredicate correctly.
I have tried the following:
NSString *complexPredicateFormat = [NSString stringWithFormat:@"ZREF_B == %@", self.B];
NSPredicate *filter = [NSPredicate predicateWithFormat:complexPredicateFormat];
[fetchRequest setPredicate:filter];
… but that results in the following error:
'NSInvalidArgumentException', reason: 'Unable to parse the format string "ZREF_B == <B: 0x89d8300> (entity: B; id: 0x89d3ca0 <x-coredata://...
Does anyone know how to set a NSManagedObject (updated) as predict filter (represents the where clause)?
EDIT:
I am sorry. Of course A and also B inherits from NSManagedObject and NOT NSObject.
Based on Gabriel suggestion:
Have you created a Data Model? Why your entities subclass
NSObject?Then
If
ref_foreignkey_Bis a relationship for entityByou need to useObviously if you use that predicate you need to query (use a fetch request) against
Aand notB.Some Notes
Why do you use
ZREF_B? This is the column created in the sqlite file (I suppose). You don’t have to use that but only properties (attributes or relationships) declared in the data model.When you deal with Core Data you deal with an object graph and not with the db (sqlite for example) itself. This is an important point to understand. You have not to deal with the sqlite file (for example) that is created for you. Only with the data model you have created.
In addition, I suggest you to create an inverse relationship from
BtoA. This lets you to maintain integrity in the object graph. For info read Relationships.Hope it helps.