On Feb 9, 2013, at 10:20 AM, Devarshi Kulshreshtha wrote:

> Say I have an employee entity and a company entity in core data.
> 
> So employee and company are related to each other like this:
> 
> Employee <<---> Company
> 
> Now I am trying to right a manageRelationships method in each class,
> something like this:
> 
>    @interface Employee : NSManagedObject
>    - (void)manageRelationships;
>    @property (nonatomic, retain) Company *company; // for relationship
>    @property (nonatomic, retain) NSNumber *companyId; // acts as foreign
> key
>    @end
> 
>    @implementation Employee
>    @dynamic company;
>    @dynamic companyId;
>    - (void)manageRelationships
>    {
>       // prepare a predicate as @"companyId == %@",self.companyId
> 
>       // execute a fetch request against Company entity
> 
>       // map relationship using self.company = retrievedCompanyObject
>    }
> 
> Now I have few questions:
> 
> 1. Is it safe to fire fetch request and map a relationship, as implemented
> above, within a subclass of NSManagedObject?

Possibly, though you have to be watchful of fault-firing.

> 2. Is their any better way to achieve it? (Idea behind above approach is- I
> will be calling above method on each created managed object so that it
> automatically manages and maps all associated relationships)

Well, Core Data manages modeled relationships for you given a minimal 
relationship. For instance, when creating a new Employee, simply setting 
newEmployee.companyRelationship = companyObject will establish also the to-many 
side of the relationship as long as both are modeled. You can also do the 
reverse, inserting the Employee object into the Company to-many relationship 
collection (in a KVO-compliant way). Note that this is all fully and well 
documented in the Core Data docs.

HTH,

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to