0
我有一个非常简单的模型,有两个对象:名称和类别。一个名称可以在许多类别中(这是单向关系)。我正在试图用8个名称创建8个类别。示例代码:核心数据关系问题
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];
这里还有一个问题 - 我已经检查了8类被保存,64名被保存,但只有8所有名称与任何类别连接。所以,当我在分类查询名称:
[NSPredicate predicateWithFormat:@"[email protected] != 0"]
...有8个元素,当我查询:
[NSPredicate predicateWithFormat:@"[email protected] = 0"]
...有56元。
当我使双向关系有效时。这里发生了什么?
您的实体图的描述很混乱。你的意思是你有类别<< - 名称或你有类别 - >>名称? – TechZen 2010-06-16 16:10:28