Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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):

Code:
@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:

- -What is the best way to create an account object? Will invoking + insertNewObjectForEntityForName:inManagedObjectContext and assigning an already existing department do the trick?
-- 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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.