Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData adding relationships to-many

In coredata I can work with one Entity fairly well, mostly because its nicely documented on the internet. However when i get to relationships i keep finding the same data over and over that tells me how to add one to an entity and and best practices but falls short when giving me actual useable examples.

So heres the thing, I've got a one-to-many (between entities:Name and ErgTimes) relationship set up and Im wondering how to add multiple objects to Times for every name. Inside my Name.m file i have

- (void)addTimesObject:(ErgTimes *)value;

but i dont know where i should use this to add in times.

Sorry for the lack of code in this example, but if anyone could just point me to a tutorial that shows the use of relationships so i can get an idea that would be so awesome.

-James.

like image 838
James Dunay Avatar asked Dec 16 '22 16:12

James Dunay


1 Answers

Read Custom To-Many Relationship Accessor Methods. You can use the standard -mutableSetValueForKey: method to access your relationship:

NSMutableSet *ergTimes = [person mutableSetValueForKey:@"ergTimes"];
[ergTimes addObject:newErgTime];

Or, if you need to do the above in a number of places, you might want to implement an accessor for the 'ergTimes' property so that you can add a time directly:

[person addErgTimesObject:newErgTime];

Examples are given in the section I cited above.

like image 71
Caleb Avatar answered Jan 01 '23 16:01

Caleb