views:

101

answers:

2

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 I query:

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

...there are 56 elements.

When I make the relationship two-way it works. What is going one here?

A: 

When i make relationship to way it worked. Do you have any idea why it didn't work before?

awattar
Hi awattar, welcome to StackOverflow! This site functions a bit differently than most other forums; if you have a question about an answer, you should add it as a comment on the answer directly. That's why this "answer" is getting voted down -- it's not an answer! Secondly, I highly recommend accepting answers that solve your problem as the resolution. If Marcus' answer fixed your issue, it's nice to reward his time with some reputation and credit. Finally, to this follow-up question itself: http://stackoverflow.com/questions/2974131
Matt B.
A: 

First, your relationships should always be bi-directional. If they are not you will have issues with referential integrity and performance as you have already seen.

Marcus S. Zarra