hafizito
hafizito

Reputation: 63

Setting new values for each many-to-many entity relationship in core data

This is driving me crazy and most likely I am doing this wrong.

So I have two entities: Criteria and Option that has a many to many relationship.

The attributes for each are

Option:
optionName

Criteria:
criteriaName
criteriaRank

In my app, I create all my options and set its name.

Then I insert a criteria and add that to each option :

Criteria *aCriteria = [NSEntityDescription insertNewObjectForEntityForName:@"Criteria" inManagedObjectContext:[decision managedObjectContext]];

    aCriteria.criteriaRank = [NSNumber numberWithInt:1];

// Add the new criteria to the criteria array and to the table view.    
[criteriasArray addObject:aCriteria];


NSEnumerator *enumerator = [fetchedOptions objectEnumerator];
Option *anOption;
while(anOption = [enumerator nextObject])
{
    [anOption addCriteriasObject:aCriteria];
}

What I want is that for each option, I can rank each criteria but when I load the criteria list for each option and set its rank via a slider and save, all the other options display the same value i just updated.

I am fetching the criteria in the following manner:

//fetch all criterias
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"criteriaName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    NSMutableArray *sortedCriterias = [[NSMutableArray alloc] initWithArray:[option.criterias allObjects]];
[sortedCriterias sortUsingDescriptors:sortDescriptors];
self.criteriasRankingArray = sortedCriterias;

I want to be able to add a criteria and then under each option, rank each criteria. Am I approaching this the wrong way? Help!!

Thanks.

Upvotes: 1

Views: 158

Answers (1)

logancautrell
logancautrell

Reputation: 8772

If you are adding the same Criteria objects to different Options, then you will see this behavior. You'll have to create brand new Criteria objects for each of your Option objects. It looks like you are reusing your Criteria objects across multiple Options, which does not sound like what you want. You seem to want unique Criteria for each Option object.

Upvotes: 1

Related Questions