views:

173

answers:

1

I have an AccountCredential object which holds standard user credentials and then another object that holds several of these AccountCredential objects. When I model this in CoreData, I'd like to know if AccountCredential needs to have a relationship link back to Account for every instance it holds.

Would I set it up in CoreData as this:

@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

Or is it sufficient enough for Account to have a reference to AccountCredential and no relationship link from AccountCredential to Account?

There's no reason for AccountCredential to know that it is used for two types of accounts in the 'Accounts' interface so I see it as a uni-directional reference. I understand that CoreData likes relationships to be bidirectional but I'm curious as to if this is necessary in the model or not.

A non-CoreData relationship would look like this:

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

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

If you build this, Xcode will likely give you warnings that there are no inverse relationships. CoreData really likes it when you have inverse relationships (I can't remember the reasoning but it does make sense when you really think about it).

jbrennan
Core Data uses the inverse relationship information to ensure the consistency of the object graph if a change is made. But I'm wondering if I don't have to do the inverse relationship...and if so, is there also a way to suppress this warning?
Alexi Groove