Core Data Relationship problem
Posted
by awattar
on Stack Overflow
See other posts from Stack Overflow
or by awattar
Published on 2010-06-16T09:55:01Z
Indexed on
2010/06/16
10:32 UTC
Read the original article
Hit count: 390
I have a very simple model with two objects: Name and Category. One Name can be in many Categories (it's one way relationship). I'm trying to create 8 Categories every with 8 Names. Example code:
NSMutableArray *localArray = [NSMutableArray arrayWithObjects:
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"g1", @"Name",
@"g1", @"Icon",
[NSNumber numberWithBool:YES] , @"Male",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"g2", @"Name",
@"g2", @"Icon",
[NSNumber numberWithBool:YES] , @"Male",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"g3", @"Name",
@"g3", @"Icon",
[NSNumber numberWithBool:YES] , @"Male",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"g4", @"Name",
@"g4", @"Icon",
[NSNumber numberWithBool:YES] , @"Male",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"g5", @"Name",
@"g5", @"Icon",
[NSNumber numberWithBool:YES] , @"Male",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"g6", @"Name",
@"g6", @"Icon",
[NSNumber numberWithBool:YES] , @"Male",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"g7", @"Name",
@"g7", @"Icon",
[NSNumber numberWithBool:YES] , @"Male",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"g8", @"Name",
@"g8", @"Icon",
[NSNumber numberWithBool:YES] , @"Male",
nil],
nil];
NSMutableArray *localArray2 = [NSMutableArray arrayWithObjects:
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Test1", @"Name",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Test2", @"Name",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Test3", @"Name",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Test4", @"Name",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Test5", @"Name",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Test6", @"Name",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Test7", @"Name",
nil],
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Test8", @"Name",
nil],
nil];
NSError *error;
NSManagedObjectContext *moc = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
for(NSMutableDictionary *item in localArray) {
NSManagedObject *category = [NSEntityDescription insertNewObjectForEntityForName:@"Category" inManagedObjectContext:managedObjectContext];
[category setValue:[item objectForKey:@"Name"] forKey:@"Name"];
[category setValue:[item objectForKey:@"Icon"] forKey:@"Icon"];
[category setValue:[item objectForKey:@"Male"] forKey:@"Male"];
for(NSMutableDictionary *item2 in localArray2) {
NSManagedObject *name = [NSEntityDescription insertNewObjectForEntityForName:@"Name" inManagedObjectContext:managedObjectContext];
[name setValue:[item2 objectForKey:@"Name"] forKey:@"Name"];
[[name mutableSetValueForKey:@"CategoryRelationship"] addObject:category];
}
}
[moc save:&error];
And here's a problem - i've checked that 8 Categories are saved, 64 Names are saved but only 8 from all Names are connected with any category. So when i query for Names in Categories [NSPredicate predicateWithFormat:@"CategoryRelationship.@count != 0"] there are 8 elements and when [NSPredicate predicateWithFormat:@"CategoryRelationship.@count = 0"] there are 56 elements. What is going one here?
© Stack Overflow or respective owner