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
出典
2011-12-28 05:59:29
MGA
私は本当に理解できません。関係が1-M、M-M、または1-1でない場合、それは何ですか? – paulbailey