【发布时间】:2026-01-18 16:55:02
【问题描述】:
我有一个包含标准用户凭据的 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
【问题讨论】:
标签: objective-c core-data