CoreData建模关系审查

时间:2009-09-30 17:02:57

标签: objective-c core-data

我有一个AccountCredential对象,它包含标准用户凭据,然后是另一个包含多个AccountCredential对象的对象。当我在CoreData中对此进行建模时,我想知道AccountCredential是否需要将关系链接返回到Account所拥有的每个实例。

我会在CoreData中将其设置为:

@interface Account :  NSManagedObject  
{
}

@property (nonatomic, retain) AccountCredential * twitterAccountCred;
@property (nonatomic, retain) AccountCredential * facebookAccountCred;

@end


@interface AccountCredential :  NSManagedObject  
{
}

@property (nonatomic, retain) NSString * password;
@property (nonatomic, retain) NSString * username; // encrypted
@property (nonatomic, retain) Account * account1;
@property (nonatomic, retain) Account * account2;

@end

或者,对于Account是否足以引用AccountCredential并且没有从AccountCredential到Account的关系链接?

AccountCredential没有理由知道它在“帐户”界面中用于两种类型的帐户,因此我将其视为单向引用。我知道CoreData喜欢关系是双向的,但我很好奇是否在模型中是否有必要。

非CoreData关系如下所示:

@interface AccountCredential : NSObject {
    NSString *username;
    NSString *password; //encrypted
}
@end

@interface Account : NSObject {
    AccountCredential       *twitterAccountCred;
    AccountCredential       *facebookAccountCred;
}
@end

1 个答案:

答案 0 :(得分:0)

如果你构建这个,Xcode可能会给你警告,没有反向关系。当你有反向关系时,CoreData真的很喜欢它(我不记得推理但是当你真的想到它时它确实有意义。)