views:

174

answers:

1

Hi all, 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  A: 

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];
gerry3
Thanks @gerry3, yeah I realise what I've been doing wrong. So when I'm relating multiple `Telephone` objects to a `Branch`, I need to add an `NSSet` which contains all the `Telephone` objects for that `Branch`, right?
Griffo
You can add objects individually OR add a set of objects OR change the set of objects.
gerry3