I've been looking into this issue for the last day, so I think it's time to ask the question.
I created a one-to-many self referencing relation in CoreData to store contacts and group these contacts.
object http://i57.tinypic.com/rm6ul4.png
Contacts that haven't got a parent actually hold the "section name" of its children.
Section name (object without parent)
-------------------------------------
Contact 1
Contact 2
Code to save the objects (simplified):
//Create Section
section = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:_managedObjectContext];
[section setValue:self.sectionField.text forKey:@"name"];
[section setValue:nil forKey:@"parent"];
[_managedObjectContext save:&error];
//Create contact
NSManagedObject *newContact = [NSEntityDescription
insertNewObjectForEntityForName:@"Contact"
inManagedObjectContext:_managedObjectContext];
[newContact setValue:self.nameField.text forKey:@"name"];
[newContact setValue:self.numberField.text forKey:@"data"];
[newContact setValue:[NSString stringWithFormat:@"%@",[LoginController getUser][@"id"]] forKey:@"user_id"];
[newContact setValue:section forKey:@"parent"];
Tableviewcontroller:
In viewWilAppear
I fetch all "parent"-objects (=sections) for the table view and assign them to the self.sections
property.
//In viewWillAppear I get all "parent" objects
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:_managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
// Set predicate
[request setRelationshipKeyPathsForPrefetching: [NSArray arrayWithObject:@"children"]];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(parent == nil) AND (children.@count > 0)"];
[request setPredicate:predicate];
self.sections = (NSMutableArray *)[_managedObjectContext executeFetchRequest:request error:nil];
Then in cellForRowAtIndexPath I try to retrieve the child with index at indexPath.row within the parent with index at indexPath.section.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
Contact * section = [self.sections objectAtIndex:indexPath.section];
NSArray * contacts = [section.children allObjects];
Contact * contact = [contacts objectAtIndex:indexPath.row];
NSLog(@"contact %@", contact.name);
[cell.textLabel setText:contact.name];
[cell.detailTextLabel setText:contact.data];
UIImageView *call = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Call"]];
[cell setAccessoryView:call];
return cell;
The name of the contact won't show and I'm getting kind of desperate here. Any other insights?