2010-06-16 132 views
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元。

當我使雙向關係有效時。這裏發生了什麼?

+0

您的實體圖的描述很混亂。你的意思是你有類別<< - 名稱或你有類別 - >>名稱? – TechZen 2010-06-16 16:10:28

回答

0

首先,你的關係應該是總是是雙向的。如果他們不是,那麼你將會遇到參照完整性和表現方面的問題,正如你已經看到的那樣。