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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:22:25+00:00 2026-05-13T22:22:25+00:00

I’ve been trying to get to grips with Core Data and I’m having some

  • 0

I’ve been trying to get to grips with Core Data and I’m having some difficulty with my fetched results. The problem is that I have ‘Branch’ objects which have a to-many relationship to ‘Telephone’ objects. When I return a ‘Branch’ and try to access all related ‘Telephone’ objects from the returned NSSet, I seem to only get one object back. See the code below which I’m using to try to see what’s going on.

 /***************************************************************************************
  start testing the fetched objects
  ***************************************************************************************/
 NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:nil];
 for (int i=0; i<[results count]; i++) {
     NSString *sortcode = [[results objectAtIndex:i] valueForKey:@"sortcode"];
     NSString *lat = [[results objectAtIndex:i] valueForKey:@"latitude"];
     NSString *lon = [[results objectAtIndex:i] valueForKey:@"longitude"];
     NSSet *phoneSet = [[results objectAtIndex:i] valueForKey:@"telephone"];

     int phoneCount = [phoneSet count];
     NSArray *phoneArray = [self arrayFromSet:[[results objectAtIndex:i] valueForKey:@"telephone"]];
     for (int j=0; j<[phoneArray count]; j++) {
         NSString *phoneNumber = [[phoneArray objectAtIndex:j] valueForKey:@"number"];
         NSLog(@"%@,%i,%i",phoneNumber,[phoneArray count],phoneCount);
     }
     NSLog(@"%@,%@,%@",sortcode,lat,lon);
 }
 /***************************************************************************************
  finish testing the fetched objects
  ***************************************************************************************/

This results in the following output:

2010-03-05 22:05:18.566 AIB[3175:207] 059 9151727,1,1
2010-03-05 22:05:18.566 AIB[3175:207] 933325,52.802288,-6.737655

Here’s where I add the objects to the context:

// Add all telephones to this branch
for (int i=0; i<[telephoneArray count]; i++) {
    [newTelephone setBranch:newBranch];
    [newTelephone setNumber:[[telephoneArray objectAtIndex:i] valueForKey:@"number"]];
    [newBranch addTelephoneObject:newTelephone];
    NSLog(@"i=%i and phone number=%@", i, [newTelephone valueForKey:@"number"]);
}

NSError *error;
if (![managedObjectContext save:&error]) {
    // Handle the error.
    NSLog(@"Save failed with error %@",error);
} else {
    NSLog(@"Save was successful");
}

…and here’s the output of the above

2010-03-05 22:15:03.217 AIB[3175:6837] i=0 and phone number=059 9151204
2010-03-05 22:15:03.218 AIB[3175:6837] i=1 and phone number=059 9151179
2010-03-05 22:15:03.218 AIB[3175:6837] i=2 and phone number=059 9151727
2010-03-05 22:15:03.231 AIB[3175:6837] Save was successful
  • 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-13T22:22:25+00:00Added an answer on May 13, 2026 at 10:22 pm

    The problem is in this code:

    // Add all telephones to this branch
    for (int i=0; i<[telephoneArray count]; i++) {
        [newTelephone setBranch:newBranch];
        [newTelephone setNumber:[[telephoneArray objectAtIndex:i] valueForKey:@"number"]];
        [newBranch addTelephoneObject:newTelephone];
        NSLog(@"i=%i and phone number=%@", i, [newTelephone valueForKey:@"number"]);
    }
    

    The newTelephone object is the SAME for every iteration of the for loop.
    Thus, this line is only adding one object to the set (it is added multiple times, but an NSSet doesn’t allow duplicates and that’s really not what you want anyway):

    [newBranch addTelephoneObject:newTelephone];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker

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.