I have the following model:
开发者_JS百科Doctor <-->> Case <->> Report
A Doctor has many Cases but a Case has only one Doctor. A Case can has many Reports
I successfully generated the classes representing my model, and I inserted one Doctor, with one Case with one Report.
How can I return to this Doctor and associate another Case and the same for a Report?
Any example for one-to-many Core Data insertion and display.
best regards
When you insert a managed object you get a pointer to that object back. Assuming you have a NSManagedObject subclass of Doctor, you would create a new Doctor object like this:
Doctor *newDoc=[NSEntityDescription insertNewObjectForEntityForName:@"Doctor"
inManagedObjectContext:myManagedObjectContext];
newDoc
is now a pointer to the Doctor
object and you can treat it like any other object. You can retain a reference to it any way you like e.g. instance attribute, array, set etc. You can then retrieve it and add new Case object like so:
[newDoc addCaseObject:aNewCase];
...and the same to add a Report to a case. These methods are defined in the autogenerated subclasses.
精彩评论