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

  • Home
  • SEARCH
  • 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 6057973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:32:50+00:00 2026-05-23T08:32:50+00:00

I have an entity class, correctly defined in a managed context. This class has

  • 0

I have an entity class, correctly defined in a managed context.
This class has a one-to-many relationship with another class.

Xcode 4 graphic facilities correctly created the derived classes, and the relationship is represented by a NSSet.

I am wondering how the creation of the relation classes is managed.
I mean, for creating the main entity I am using the

NSManagedObject *newEntity = [NSEntityDescription
insertNewObjectForEntityForName:@"EntityName"
inManagedObjectContext:context];

But what about the relationship in NSSet ? Do I need to create it in the same way as a parent entity and store it regularly in NSSet ?

NSManagedObject *child = [NSEntityDescription insertNewObjectForEntityForName:@"ChildName" inManagedObjectContext:context];

NSSet *childSet = [..set creation with child..];
newEntity.child = childSet;

// save newEntity in context

If yes, because NSSet is an object why doesn’t it need to be created starting from the context ? Such question could be applied to all the ‘normal’ properties in the entity, an NSString is an object too, why a simple newEntity.prop=@”” is enough ?

  • 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-05-23T08:32:50+00:00Added an answer on May 23, 2026 at 8:32 am

    For to-many relationships NSManagedObject has a mutableSetValueForKey: method that returns the set you would use. With newEntity and child defined as above you’d do something like this:

    NSMutableSet *childObjects = [newEntity mutableSetValueForKey:@"childRelationship"];
    [childObjects addObject:child];
    

    But you said you had Xcode generate custom subclasses for your Core Data entities, so you have a convenience method defined in the Parent class which would be named something like addChildObject:. Using that you could replace the above with a simpler version, but you’ll also need to declare newEntity as an instance of this subclass instead of as a generic NSManagedObject:

    Parent *parent = [NSEntityDescription insertNewObjectForEntityForName:@"EntityName" inManagedObjectContext:context];
    NSManagedObject *child = [NSEntityDescription insertNewObjectForEntityForName:@"ChildName" inManagedObjectContext:context];
    
    [parent addChildObject:child];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
In my business model I have Entity class (IPoint interface ) that has a
I have an entity class that has a property with an underlying db column
I have an entity class that has 10 fields. I am using MS Validation
This has been driving me nuts for a week now. I have a class
I have a many to many relationship between a Team and an Employee entity.
I have an entity that is mapped with a <join> element like this: <class
I have an entity class Foo foo that contains Collection<Bar> bars . I've tried
I have an Entity class, which contains 3 pointers: m_rigidBody, m_entity, and m_parent. Somewhere
I have two entity classes annotated in the following way @Entity class A {

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.