creating managed objects using code in xcode & core-data
New to objective-c xcode and core-data so sorry for the remedial question.
I have set up a very simple data model:
Entity1 and Entity2, both contain a single attribute (String) and a one-to-many relationship with the other.
I want to scan Entity1 and depending on the results of the scan create one or more Entity2 objects that link to Entity1.
How can I do this? I don’t understand how I create Entity2 type objects in code and how I would define the relationship to the Entity1 object they are related to.
I come from a SQL programming background where inserting elemen开发者_开发技巧ts into the Entity2 table with the ID of the related Entiry1 entry is easy. I can’t get my head around the xcode core-data abstraction and would appreciate any help.
Just create an "Entity2" object using -NSEntityDescription insertNewObjectForEntityForName: inManagedObjectContext:
, then establish the relationship between "Entity1" and "Entity2" by assigning to the property. For example, if Entity2 has a "parent" property, referring to Entity1, you might do:
e2 = [NSEntityDescription
insertNewObjectForEntityForName:@"Entity2"
inManagedObjectContext:context];
e2.parent = e1; // this will create the relationship in both directions
精彩评论