Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent Foreign Key relationships in Core Data - Data Model in XCode

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.

like image 223
calamandurrio Avatar asked Dec 22 '11 11:12

calamandurrio


People also ask

How do you add relationships in Xcode?

Open the Data Model inspector (choose View > Inspectors > Show Data Model Inspector). Select the source entity from the Entities list, then select the new relationship in the Relationships list.

What are relationships in Core Data?

CoreData entity relationships help us to associate records of one entity with the records of another entity. Let us understand it with the help of an example. In our Blogger project, we have CoreData entities namely User and Post.

How do I get NSManagedObject?

Still inside the Core Data editor, go to the Editor menu and choose Create NSManagedObject Subclass. Make sure your data model is selected then click Next. Make sure the Commit entity is checked then click Next again.

What are fetched properties in Core Data?

Fetched Properties in Core Data are properties that return an array value from a predicate. A fetched property predicate is a Core Data query that evaluates to an array of results.


1 Answers

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:@"[email protected]"];

//...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

like image 91
MGA Avatar answered Nov 03 '22 16:11

MGA