I am having a hard time figuring out some things about how Core Data handles its database. I will explain my problems with a concrete example:
Let's just say that we have the following schema where there are departments which hold numerous accounts (our addresses).
I have created my Core Data file using the editor. In the CachedDepartment class, I have added the necessary attributes, and I have created a to-many relationship, and I have selected "Inverse" to the "Department property of my CachedAccount. CacheAccount also has attributes, and a relationship inverse to the "addresses" relationship of CachedDepartment.
I have the following sources (I give you the 2 header files. The rest contain nothing of interest):
@interface CachedAccount : NSManagedObject {
@private
}
@property (nonatomic, retain) NSNumber * accountID;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) CachedDepartment *department;
@end
@class CachedAccount;
@interface CachedDepartment : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * departmentID;
@property (nonatomic, retain) NSString * departmentName;
@property (nonatomic, retain) NSSet *addresses;
@end
@interface CachedDepartment (CoreDataGeneratedAccessors)
- (void)addAddressesObject:(CachedAccount *)value;
- (void)removeAddressesObject:(CachedAccount *)value;
- (void)addAddresses:(NSSet *)values;
- (void)removeAddresses:(NSSet *)values;
@end
And now the questions:
-- How should an account and department objects be created so that a one-to-many relationship will be established?
-- What should I do with CoreDataGeneratedAccessors?After creation, should I call the -addAddressesObject function of CachedDepartment or I will need to make something like anAccount.department = aDepartment and that's it?
-- In each case when creating objects how can I make sure that adding an address to a department won't create a double instance of the address and store it?
I would appreciate any insights on this.
EDIT:
Should inserting new objects for CachedDepartment entity look like the following code:
NSManagedObject *cachedDepartment= [NSEntityDescription
insertNewObjectForEntityForName:@"CachedDepartment"
inManagedObjectContext:[self managedObjectContext]];
/*
set values for failedBankInfo here
*/
NSError *error;
if (![context save:&error]) {
NSLog(@"The following error occured: %@", [error localizedDescription]);
}
then would adding a new record for the cachedAccounts which will be in one-to-many relationship with the newly inserted cachedDepartment object be something like that:
NSManagedObject *cachedAccounts = [NSEntityDescription
insertNewObjectForEntityForName:@"CachedAccounts"
inManagedObjectContext:[<b>cachedDepartment managedObjectContext</b>]];
/*
set values for failedBankInfo here
*/
//setting the one-to-many relationship
[cachedDepartment addCachedAccountObject:cachedAccount];
[cachedAccount setCachedDepartment:cachedDepartment];
//
NSError *error;
if (![context save:&error]) {
NSLog(@" couldn't save: %@", [error localizedDescription]);
How should an account and department objects be created so that a one-to-many relationship will be established?
Core data will automatically populate the inverse relationship for you if you have configured it in the model. So, depending on how you are structuring your code, you can just put newAccount.department = aDepartment;
and your relationship and inverse are both created.
What should I do with CoreDataGeneratedAccessors?After creation, should I call the -addAddressesObject function of CachedDepartment or I will need to make something like
anAccount.department = aDepartment
and that's it?
Yes, that's it. See previous sub-answer. The generated accessors are in case the flow of your program makes it more sensible to work the other way around, or if you haven't defined an inverse relationship.
You could do
[aDepartment addAddressesObject:anAccount];
instead of
anAccount.department = aDepartment;
but you don't need to do both.
In each case when creating objects how can I make sure that adding an address to a department won't create a double instance of the address and store it?
If you did call both methods, this would have no effect, since the relationship is stored as an NSMutableSet
and you can't add the same object to a set more than once.