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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:27:57+00:00 2026-05-20T21:27:57+00:00

Does anyone have a tutorial or source code that shows a to-many relationship being

  • 0

Does anyone have a tutorial or source code that shows a to-many relationship being used, where the user add elements in on the fly? I would just like to have a look because reading about it hasnt been much help.

  • 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-20T21:27:58+00:00Added an answer on May 20, 2026 at 9:27 pm

    People are often confused by to-many relationships because one entity represents the relationship as a set while the other represents it as a single object. Take the following entities:

    EntityA{
        name:string
        bees<-->>EntityB.a
    }
    
    EntityB{
        name:string
        a<<-->EntityA.bees
    }
    

    In EntityA, the relationship bees is a set because there maybe many EntityB objects in the relationship. So, using Key-Value coding, you would have to access the relationship using a mutableSetForKey: expanding everything out to see the detail would like so:

    NSMutableSet *muteSet=[anEntityAObj mutableSetValueForKey:@"bees"];
    [muteSet addObject:aNewBObj];
    [anEntityAObj setValueForKey:@"bees"];
    

    …or more compactly:

    [[anEntityAObj mutableSetValueForKey:@"bees"] addObject:aNewBObj];
    

    If you set from the EntityB side, however, you are only adding a single object so you can just use setValueForKey: directly like so:

    [anEntityBObj setValueForKey:anEntityAObj];
    

    That’s if you use generic NSManagedObject instances to represent your entities. If you create custom subclasses then you have properties and methods to do the setting for you:

    [anEntityAObj addBeesObject:anEntityBObj];
    anEntityBObj.a=anEntityAObj;
    

    Remember as well that with managed objects, setting one side of a relationship defined as reciprocal automatically set the other side as well and removing works the same way.

    Update

    Lets say i’ve got 2 entities —
    Person:with “name” attribute —
    Times:with “time” attribute — i would
    want to set multiple times for each
    name, but how to I tell it which name
    i would like to add the specific times
    to?

    You don’t create relationships with attributes, in this case name but rather with an object, in this case an instances of the Person entity/class. Each individual Person object is completely separate from all other Person objects even if they have the same value in their name attribute.

    You must obtain a reference to any particular Parent object. If you have just inserted a new Parent object then you already have a reference to it. If it is already been inserted/persisted, then you create a fetch with a predicate that will return the proper object. Once you have the correct Parent object you then just add the Time objects to the relationship.

    So, if your entity looks like this pseudo-code:

    Parent{
        name:string
        times<-->>Time.parent
    }
    
    Time{
        theTime:date
        parent<<-->Parent.times
    }
    

    … and you are using generic NSManagedObjects to instatiate you entities, you set the relationship between an existing Parent object and new Time object something like this:

    NSManagedObject *existingParent= //... results of a fetch
        NSManagedObject *newTime=[NSEntityDescription insertNewObjectForEntityForName:@"Time" inManagedObjectContext:self.moc];
        [newTime setValue:[NSDate date] forKey:@"theTime"];
        [newTime setValue:existingParent forKey:@"parent"];
    

    Note that if you set the relationship from the Time object’s side, you can use setValue:ForKey: because from the Time object’s perspective the relationship is just one object to one object.

    It is really quite simple once you start thinking in objects instead of databases. Each object you insert in a context is unique even if it shares attributes with other objects of the same entity/class. That is why you can set a relationship between specific objects without necessarily worrying about the values stored in their attributes.

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

Sidebar

Related Questions

Does anyone have a good technique (or tutorial) to implement rulers within a C#
Does anyone have a good articles or tutorial on correctly using dispose and IDisposable.
Is there a good tutorial or does anyone have experience with setting this up
Does anyone have any recommendations of tools that can be of assistance with moving
Does anyone have links to tutorials regarding the new IModelBinder in asp.net mvc beta?
Does anyone have a definitive answer to whether Sql Server Management Objects is compatible
Does anyone have the secret formula to resizing transparent images (mainly GIFs) without ANY
Does anyone have a technique for generating SQL table create (and data insert) commands
Does anyone have a good way to build MSI (vdproj) projects using MsBuild or
Does anyone have any experience getting MSTest to copy hibernate.cfg.xml properly to the output

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.