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 8736393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:17:40+00:00 2026-06-13T10:17:40+00:00

I have subclassed NSManagedObjects for a Person and an Company. In core data, I

  • 0

I have subclassed NSManagedObjects for a Person and an Company. In core data, I have set these up with a two way relationship (one from person to company called personCompany, one the other way around companyPerson). My App saves both a Person and a Company beautifully, their header files look as follows (massively simplified for demo purposes):

#import <CoreData/CoreData.h>

@interface BBPerson : NSManagedObject

@property (nonatomic) NSString *firstName;
@property (nonatomic) NSString *lastName;

@end

----------------------

#import <CoreData/CoreData.h>

@interface BBCompanyName : NSManagedObject

@property (nonatomic) NSString *companyName;

@end

In my “Add Person” view controller, part of the save method is below.

    //If this is a new person, best add a person
    if (!currentPerson) {
        currentPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person"
                                                         inManagedObjectContext:managedObjectContext];
    }

    //Write the values back to the entity

    currentPerson.firstName = txtFirstName.text;
    currentPerson.lastName = txtLastName.text;

**//I WANT TO DO THE BELOW: but clearly can't**    
//[currentPerson setCompany:selectedCompany];.

    //Save back to core data
    NSError *error;
    if (![managedObjectContext save:&error]) {
        NSLog(@"Failed to save - error: %@", [error localizedDescription]);
    }

If I had another Company referenced here (show above as selectedCompany), how can I change my subclassed NSManagedObject to add this relationship? I have looked at the documentation and I am still a bit adrift.

What I am trying to do, with my subclassed NSManagedObject is to create a relationship that can be persisted. Something like [currentPerson setCompany:selectedCompany]; which would of course require on the currentPerson a method – how would this method look?

What I would like to do is be able to at a later stage in the app is to call all the People back for a Company. I did think about doing this using predicates and foreign keys and ditch relationships, but that is me getting myself in the wrong mindset that this is a database, which it is not.

  • 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-13T10:17:41+00:00Added an answer on June 13, 2026 at 10:17 am

    You want to set up your entities in the core data editor (comes up when you select your xcdatamodel in the project navigator).

    Add attributes like name, phoneNumber, etc. for both Person and Company entities.

    Then for the Company entity, under relationships, add a relationship with the name people, destination Person. With that relationship selected, in the data model inspector pane, tick the box that says to-many relationship. For the Person entity, add a relationship called company, set its destination to Company, and its inverse to people.

    Delete your NSManagedObjectModel subclasses from the project navigator. Create a new file and select Core Data from the left menu. Select managed object subclass. It will ask you what data model you want to generate entities for. Select your data model and the entities that you want to generate NSManagedObject subclasses for (Person and Company).

    Then, you can look at the header and implementation files that Xcode creates for you and you will see that the attributes you set for your entities have been declared as properties in your header files and in the implementation they have all been marked as dynamic. More importantly, the same is true for the one-to-many relationship between Company and Person. You will see in the Company.h that Company has a @property people which is an NSSet. This is also marked @dynamic in your implementation. @dynamic does not create getters and setters for you like @synthesize does, but you can still use dot notation for them.

    Also, notice that core data generates some accessor methods for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two objects with a relationship in my Data Model... Person {Name, position,
I have a data model which has two entities in a relationship of one
In core data if we have multi value relationship, such as when a business
In my Core Data model, I've got a relationship called listItems which links to
I have two entities, one called Group and the other called Contact . They
It is possible to have one NSManagedObject subclass represent multiple Core Data entities. For
So I'm fairly new to Core Data and KVO, but I have an NSManagedObject
I have 3 entities with one being the parent of the other two. I
My NSTreeController is setting Core Data parent attributes but not children attributes. I have
I have a Core Data model entity NoteObject that has a transformable type arrayOfTags

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.