1

I am new to core data and trying to create a simple apps using core data. I am currently working on app to save data in to-many relationship, there are several questions and tutorials but I am still confused.

I have two entities person and contactNumbers, I am fetching person and its contact numbers from address book, person A has mobile number, iphone number, home, work, other... which creates to-many relationship.

In my code I didnt subclass any entity, Is it necessary to subclass entities to save data in relationship? I am asking this because I have read that its not necessary to subclass direct working with NSManagedObject class will do the job.

All I want is to save person A first name, last name in person entity and its contact numbers in contactNumbers entity. How to save data in to-many relationship using core data?

Please I request do not refer other stackoverflow questions, raywenderlich tutorial, app code blog tutorials etc.

Please provide a clearly understandable code with concepts to deal this situation. Thanks a lot.

This is my model

This is my model

if I Do this

NSManagedObject *person = [NSEntityDescription insertNewObjectForEntityForName:@"person" inManagedObjectContext:self.managedObjectContext];

[person setValue:@"first name A" forKey:@"firstName"];
[person setValue:@"last name B" forKey:@"lastName"];
[person setValue:@"123" forKey:@"mobile"];

I got this error

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSManagedObject 0x82abcc0> setValue:forUndefinedKey:]: the entity Person is not key value coding-compliant for the key "mobile".

If i do this

NSManagedObject *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];
    [person setValue:@"first name A" forKey:@"firstName"];
    [person setValue:@"last name B" forKey:@"lastName"];    

    NSManagedObject *contactNumber = [NSEntityDescription insertNewObjectForEntityForName:@"ContactNumber" inManagedObjectContext:self.managedObjectContext];
    [contactNumber setValue:@"123" forKey:@"home"];
    [contactNumber setValue:@"456" forKey:@"iPhone"];
    [contactNumber setValue:@"789" forKey:@"mobile"];
    [contactNumber setValue:@"111" forKey:@"work"];
    [contactNumber setValue:@"112" forKey:@"other"];

its working fine, but how data got related to each other in two entities?

4

2 回答 2

3

You don't need to subclass.

You just need relations.

Make a relation on Person named contactNumbers that points to the ContactNumber entity, and a matching relation on ContactNumber entity called person that points back to person.

The contactNumber relation should be one to many (because one person has many contacts), while the relation from ContactNumber going back to Person should be one to one (because each contact entry will appear in only one person).

When you get a Person entity then, you'll be able to get a set of ContactNumber objects from the contactNumbers property, and then look through elements in the set. Or you can search ContactNumber entities where the person ID matches the person relation going back to Person.

Followup: Your first code does not work because you cannot reference a single instance through a set.

Your second section of code is fine, just add the line

contactNumber.person = person;

Then you can access the set of contacts from a person at any time with:

NSSet *contacts = person.contactNumbers;

And get the contact number you want. You cannot go directly from a person to one contact without some very tricky keypath stuff.

I would EXTREMELY STRONGLY suggest using Mogenerator to generate data objects after you set up the model, it makes the data clearer as to how you can use it. Using the setKey mechanism with Core Data is UTTERLY INSANE.

于 2013-03-30T20:20:47.040 回答
1

You do not need to do any subclassing at all to read and write Core Data relationships (or any other values for that matter). In Core Data, a relationship is represented as an NSMutableSet. Here is some sample code to show you how to retrieve this set and modify it:

// Assuming myPerson is an NSManagedObject instance of the Person entity
NSMutableSet *contactNumbers = [myPerson mutableSetValueForKey:@"contactNumbers"];

// Add numbers normally by calling the set's addObject property
[contactNumbers addObject:myNewNumber];
//etc.

I would also highly recommend taking a look at the Sensible TableView framework if you haven't already done so. The framework will automatically generate the UI for you Core Data entities, including all relationships. It will also automatically handle adding/removing new relationship objects. I myself wouldn't imagine going back to doing stuff manually again without it. Good luck!

于 2013-03-30T21:59:01.923 回答