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
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?