核心数据关系问题

时间:2010-06-16 09:55:01

标签: iphone objective-c core-data

我有一个非常简单的模型,有两个对象:名称和类别。一个名称可以在许多类别中(它是单向关系)。我正在尝试使用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:@"CategoryRelationship.@count != 0"] 

...有8个元素,当我查询时:

[NSPredicate predicateWithFormat:@"CategoryRelationship.@count = 0"] 

......有56个元素。

当我双向建立关系时,它有效。这里有什么?

1 个答案:

答案 0 :(得分:0)

首先,您的关系应 始终 是双向的。如果不是,那么您将看到参考完整性和性能方面的问题。