Question

I am obviously new in Core Data, I have read quite a lot about the topic from Apple and from countless other sources. However, none seems to treat the many-to-one relation in a way for a newbie like me to understand. I seems to be fine with one-to-one relation, but I have some difficulties on understanding many-to-one relation.

My problem is as follow:

I am working on a small project to practice Core Data. In my project, there are two entities:Person and Note and they are related as follow: (Sorry, couldn't add the picture)

A Person can have several notes, but Note can only have one owner, Person. Person entity is related with a one-to-many relation (double arrows pointing to the entity: Note), while the entity Note is related with a many-to-one (single arrow pointing to Person)

This is what I have so far:

Note.h

 @class Person;

 @interface Note : NSManagedObject

@property (nonatomic, retain) NSDate * noteCreateDate;
@property (nonatomic, retain) NSString * noteUser;
@property (nonatomic, retain) Person *person;

Person.h

  @class Note, Organise;

  @interface Person : NSManagedObject

  @property (nonatomic, retain) NSString * personAddress;
  @property (nonatomic, retain) NSData * personAvatar;
  @property (nonatomic, retain) NSString * personBDay;
  @property (nonatomic, retain) NSDate * personFutureDate;
  @property (nonatomic, retain) NSString * personGender;
  @property (nonatomic, retain) NSString * personName;
  @property (nonatomic, retain) NSString * personRelation;
  @property (nonatomic, retain) NSSet *notes;
  @property (nonatomic, retain) NSSet *organises;
  @end

  @interface Person (CoreDataGeneratedAccessors)

  - (void)addNotesObject:(Note *)value;
  - (void)removeNotesObject:(Note *)value;
  - (void)addNotes:(NSSet *)values;
  - (void)removeNotes:(NSSet *)values

In my view controller I am trying to save the notes as follow:

   NSManagedObjectContext *context = [self managedObjectContext];
   Note *newNote;
    newNote = (Note *) [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:context];

   // Saving the notes
   newNote.noteUser = noteTextView.text;
   newNote.noteCreateDate = added;
   [newNote setPerson:newNote.person];

  ///////////////Saving to the DataBase /////////
   NSError *error = nil;

   if ([self.managedObjectContext hasChanges]) {

    if (![self.managedObjectContext save:&error]) { //save failed
        NSLog(@"Save Failed: %@", [error localizedDescription]);
    }
    else {
        NSLog(@"Save Succeeded");
    }
}

It is saving the data in the database fine, but the problem is: There is no relationship between Person and Notes in the database. The column for person in the database is empty (not showing to whom the note belong to).

I have tried several things to try to link the person with the note, NONE of them seems to work. So, it seems that I am stucked and I would very much appreciate some help.

This is the latest tutorial I used for help: http://brandontreb.com/core-data-quicktip-inverse-relationships https://itunes.apple.com/fr/itunes-u/advanced-iphone-development/id407243028?l=en&mt=10

Thank you.

Was it helpful?

Solution

Your code does not create any relationship between the Note and a Person. This:

[newNote setPerson:newNote.person];

is the same as newNote.person = newNote.person; and does not change anything. You must create a Person object (or fetch an existing Person) and then set

newNote.person = thePerson; 

to establish the relationship.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top