简体   繁体   中英

Core Data: Subclassed NSManagedObject and Relationships

I have subclassed NSManagedObjects for a Person and an Company. In core data, I have set these up with a two way relationship (one from person to company called personCompany, one the other way around companyPerson). My App saves both a Person and a Company beautifully, their header files look as follows (massively simplified for demo purposes):

#import <CoreData/CoreData.h>

@interface BBPerson : NSManagedObject

@property (nonatomic) NSString *firstName;
@property (nonatomic) NSString *lastName;

@end

----------------------

#import <CoreData/CoreData.h>

@interface BBCompanyName : NSManagedObject

@property (nonatomic) NSString *companyName;

@end

In my "Add Person" view controller, part of the save method is below.

    //If this is a new person, best add a person
    if (!currentPerson) {
        currentPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person"
                                                         inManagedObjectContext:managedObjectContext];
    }

    //Write the values back to the entity

    currentPerson.firstName = txtFirstName.text;
    currentPerson.lastName = txtLastName.text;

**//I WANT TO DO THE BELOW: but clearly can't**    
//[currentPerson setCompany:selectedCompany];.

    //Save back to core data
    NSError *error;
    if (![managedObjectContext save:&error]) {
        NSLog(@"Failed to save - error: %@", [error localizedDescription]);
    }

If I had another Company referenced here (show above as selectedCompany), how can I change my subclassed NSManagedObject to add this relationship? I have looked at the documentation and I am still a bit adrift.

What I am trying to do, with my subclassed NSManagedObject is to create a relationship that can be persisted. Something like [currentPerson setCompany:selectedCompany]; which would of course require on the currentPerson a method - how would this method look?

What I would like to do is be able to at a later stage in the app is to call all the People back for a Company. I did think about doing this using predicates and foreign keys and ditch relationships, but that is me getting myself in the wrong mindset that this is a database, which it is not.

You want to set up your entities in the core data editor (comes up when you select your xcdatamodel in the project navigator).

Add attributes like name, phoneNumber, etc. for both Person and Company entities.

Then for the Company entity, under relationships, add a relationship with the name people, destination Person. With that relationship selected, in the data model inspector pane, tick the box that says to-many relationship. For the Person entity, add a relationship called company, set its destination to Company, and its inverse to people.

Delete your NSManagedObjectModel subclasses from the project navigator. Create a new file and select Core Data from the left menu. Select managed object subclass. It will ask you what data model you want to generate entities for. Select your data model and the entities that you want to generate NSManagedObject subclasses for (Person and Company).

Then, you can look at the header and implementation files that Xcode creates for you and you will see that the attributes you set for your entities have been declared as properties in your header files and in the implementation they have all been marked as dynamic. More importantly, the same is true for the one-to-many relationship between Company and Person. You will see in the Company.h that Company has a @property people which is an NSSet. This is also marked @dynamic in your implementation. @dynamic does not create getters and setters for you like @synthesize does, but you can still use dot notation for them.

Also, notice that core data generates some accessor methods for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM