Mark
Mark

Reputation: 113

Beginner Core Data and Relationships

Trying really hard to get my head around core data and relationships. My current entity setup is this:

Profiles <-->>Events

I have everything working ( I can add, change and delete profiles and events right now, I just don't have the relationship connection ). So based on this, I should just have to make the relationship "connection" once, right? When I add an event, it just needs to know which profile to associate with, and then any changes, deletes, etc will be handled by core data?

If I'm on the right track, where do I implement updating the profile when adding an event?

I'm saving the events based on delegation from the table view controller...

FROM THE ADD EVENT VIEW CONTROLLER


AddEventViewController.m

[self.currentEvent setEventdesc:eventDescField.text]
[self.delegate addEventViewControllerDidSave];



FROM THE EVENT TABLE VIEW CONTROLLER

EventTableViewController.m

-(void) addEventViewControllerDidSave {
  NSError *error = nil;
  NSManagedObjectContext *context = self.managedObjectContext;

    if (![context save:&error]) {
      NSLog(@"Error %@", error);
    }

   [self dismissViewControllerAnimated:YES completion: NIL];
}

Upvotes: 0

Views: 77

Answers (1)

Alex
Alex

Reputation: 1051

Firstly, I guess you don't have the relation in core data. Then, you need to add the relationship between them:

  1. Create one relationship named eventRelation in profile with destination Events
  2. Do the same for profiles (profileRelation) with destination profile and set his inverse. The inverse is the name of the property in the other entity, in this case the inverse of eventRelation in Profile is profileRelation.

Now you need to update the files your models adding those properties to them. For example in Profiles.m add: @property (nonatomic, retain) Events * eventRelation.

Lastly, in the code, when you want to save the event relation you will need to do:

Profile * newProfile = (Profiles *)[NSEntityDescription insertNewObjectForEntityForName:@"Profile" inManagedObjectContext:self.managedObjectContext];

Events * addEvent = (Events *)[NSEntityDescription insertNewObjectForEntityForName:@"Events" inManagedObjectContext:self.managedObjectContext];

//Set event and profile data

newProfile.eventRelation = addEvent;

//And save changes  
[self.managedObjectContext save:&error];

Now, each time you'll fetch the entity, you will able to access to the other object just using:

//fetch data into self.profiles
profiles.eventRelation

Updated with example:

Let's say you have Entity profile with attributes: name, lastname and relationship eventRelation, and Event Entity with attributes name, date and relationship profileRelation. Then, if you want to add a date in event, you can fetch the event you want and do: Events * event ... fetch it; event.date = [NSDate ...]

The other option is: You have fetched a profile and you want change the profile name and the date of one event (you already have a event in that profile), you do:

Profile * profile ... fetch it
profile.name = @"asd"
profile.eventRelation.date = [NSDate ...];

After that, you just need to save it.

Upvotes: 1

Related Questions