如何在Core Data中表示外键关系 - XCode中的数据模型 [英] How to represent Foreign Key relationships in Core Data - Data Model in XCode

查看:222
本文介绍了如何在Core Data中表示外键关系 - XCode中的数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Core Data的全新功能,我正在制作我的数据模型。我有33个实体,他们之间很少有硬关系,但很多外键关系。

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.

如何管理并非完全一对多或1-1或多对多的关系,而是核心数据模型中的外键?

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?

例如,我有一个Contact实体,它与contact_x_mail有关系,同时contact_x_mail与包含所有电子邮件的Mail有关系。这种关系是一对多或多对多。但是还有其他一些类似机构(一个联系人可以有很多机构)和邮件,不是一对多或1-1关系,机构有一个ForeignKey_mail_id。

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.

推荐答案

您正在考虑CoreData在DBMS方面,而不是。您不需要设置外键以在CoreData中建立关系。如果要向用户分配电子邮件,只需创建两者之间的关系,即可设置用户的属性电子邮件或电子邮件的用户属性。外键和链接都是由CoreData在后台进行的。

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.

在另一点上,每个关系按照定义1-1,1- *或 - 。我不确定是否有任何其他选择...

On another point, every relationship is by definition, 1-1, 1-*, or -. I'm not sure there is any other choice...

当您在CoreData中创建关系时,您将有效地为此项目创建新的属性。这里是一个例子:

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;

设置方法如下:

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.

希望这有助于清理一些事情!阅读文档以确保CoreData适合您!

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

这篇关于如何在Core Data中表示外键关系 - XCode中的数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆