Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9281699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:11:07+00:00 2026-06-18T18:11:07+00:00

Say I have an employee entity and a company entity in core data. So

  • 0

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?

  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)

Please suggest.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:11:08+00:00Added an answer on June 18, 2026 at 6:11 pm

    I see what you are trying to do. But don’t forget, Core Data provides you with an object graph, the database being only a secondary implementation detail. Thus, the whole notion of foreign keys is completely unnecessary, as the database layer is going to manage that for you.

    But alas, I assume you just do not have the Company object around when you create the employee but are just given the company id. Of course you can override the NSManagedObject subclass, maybe best in the setter of the id property:

    -(void)setCompanyId:(NSNumber*)newId {
       self.companyId = newId;
       if (!newId) { self.company = nil; }
       else {
          Company *myCompany = ... // fetch company
          self.company = myCompany;
       }
    }
    

    However, you can see how this is redundant. There is no need to keep the foreign key in your entity. You could retrieve it any time with

    NSNumber *companyId = employee.company.id;
    

    So maybe it is better to simply add a method to your Employee class that conveniently adds a company:

    -(void)addCompanyWithId:(NSNumber*)companyID {
       if (!companyID) { self.company = nil; }
       else {
          Company *employer = ...// fetch company
          self.company = employer ? employer : nil;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a Company that has many Employees and each Employee can
Let's say I have a table Employee like this EmpID, EmpName 1 , hatem
Say we have a entity Company that has a to-many relationship to the Employee
Say I have this XML variable: declare @xml xml='< Employee EmployeeId=1 FirstName=John LastName=Smith />
Lets say you have an employee table and a jobs list table. Each employee
Lets say I have a table called Employees , and each employee has a
i have an xml <Data> <employee> <name>emp1</name> <id>1</id> </employee> </Data> lets say i have
Let us say that I have a table structured like this(using SQL server): empID
I have two tables, both named as say, Employee in two different schema HR
Lets say have this immutable record type: public class Record { public Record(int x,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.