问题
Totally new with Core Data, I'm making my data model. I have 33 entities, and few hard relationships between them but lots of Foreign-Key relationships.
How can I manage that relationships that are not exactly 1-many or 1-1 or many-many but are foreign keys in Core Data Model?
For ex., I have a Contact entity, which has a relationship with a contact_x_mail, and at the same time contact_x_mail has a relationship with Mail, that contains all emails. This relationships are 1-many or many-many. But there are others like Institution (a contact can have many Institutions) and Mail, that is not a 1-many or 1-1 relationship, Institution has a ForeignKey_mail_id.
How can I represent that foreign key relationships? Indexes?
Thank you very much, hope my question is clear.
回答1:
You are thinking of CoreData in terms of a DBMS which it is not. You don't need to set up foreign keys to make relationships in CoreData. If you want to assign an email to a user you just create a relationship of between the two and you can set the attribute "email" of a user or the "user" attribute of an email. The foreignKey and linking is all done by CoreData in the background.
On another point, every relationship is by definition, 1-1, 1-*, or -. I'm not sure there is any other choice...
When you create relationships in CoreData you are effectively creating new attributes for this item. Here is an example:
@interface User : NSManagedObject
#pragma mark - Attributes
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *emailAddress;
#pragma mark - Relationships
//All to-many relationships are saved as Sets. You can add to the "emails" relationship attribute to add email objects
@property (nonatomic, strong) NSSet *emails;
//All to-one relationships are saved as types of NSManagedObject or the subclass; in this case "Institution"
@property (nonatomic, strong) Institution *institution;
Setting these is as simple as:
User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:[self.fetchedResultsController managedObjectContext]];
[user setName:@"Matt"];
[user setEmailAddress:@"matt@stackoverflow.com"];
//...Maybe i need to query my institution
NSFetchRequest *query = [[NSFetchRequest alloc] initWithEntityName:@"Institution"];
[bcQuery setPredicate:[NSPredicate predicateWithFormat:@"id == %@", institutionId]];
NSArray *queryResults = [context executeFetchRequest:query error:&error];
[user setInstitution:[queryResults objectForId:0]];
//Now the user adds a email so i create it like the User one, I add the proper
//attributes and to set it to the user i can actually set either end of the
//relationship
Email *email = ...
[email setUser:user];
//Here i set the user to the email so the email is now in the user's set of emails
//I could also go the other way and add the email to the set of user instead.
Hope this helps clear things up a bit! Read up on the documentation to make sure CoreData is right for you!
http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/CoreData.pdf
来源:https://stackoverflow.com/questions/8603224/how-to-represent-foreign-key-relationships-in-core-data-data-model-in-xcode