2011-12-22 4 views
5

コアデータで全く新しいデータモデルを作成しています。私は33の実体を持ち、それらの間にはほとんど関係はありませんが、多くの外交関係があります。コアデータの外部キー関係の表現方法 - XCodeのデータモデル

コアデータモデルでは、1-manyまたは1-1またはmany-many以外の外部キーですか、その関係をどのように管理できますか?

たとえば、contact_x_mailとの関係を持つContactエンティティがあります。同時にcontact_x_mailはMailとの関係を持ち、すべての電子メールを含んでいます。この関係は、1-manyまたはmany-manyです。しかし、Institution(連絡先は多くの機関を持つことができます)とMailのようなものがあります。これは1-manyまたは1-1の関係ではなく、機関はForeignKey_mail_idを持っています。

どのようにして外部キーの関係を表現できますか?インデックス?

ありがとう、私の質問が明確であることを願っています。

+0

私は本当に理解できません。関係が1-M、M-M、または1-1でない場合、それは何ですか? – paulbailey

答えて

8

CoreDataは、そうでないDBMSに関して考えています。 CoreDataで関係を作成するために外部キーを設定する必要はありません。ユーザーに電子メールを割り当てたい場合は、その2つの関係を作成するだけで、ユーザーの属性「電子メール」または電子メールの「ユーザー」属性を設定できます。外部キーとリンクはすべてバックグラウンドでCoreDataによって行われます。

別の点として、すべての関係は定義、1-1、1- *、または-です。

CoreDataでリレーションシップを作成すると、効果的にこのアイテムの新しいアトリビュートが作成されます。次に例を示します。

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. 

がこのビットをクリア物事を役に立てば幸い:これらの設定

@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; 

は同じくらい簡単です! CoreDataがあなたのために適切であることを確認するためにドキュメントを読んでください!

http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/CoreData.pdf

関連する問題