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]);