views:

33

answers:

1

Either I'm not understanding what the term "uniquing" means in Core Data, or I'm not fetching my data properly. I have a pretty simple data model. Three entities: Community, Asset, and Category. Each Community has a relationship to multiple categories. Each category has a relationship to multiple assets. Each asset that is created must have one and only one category.

In the code I've posted, I'd like to output all the categories that a specific community has into the console. I thought that because of Core Data's uniquing capability, only one category of the same name could exist at a time (name is the only attribute for a category). However, when I print to the console, I'm getting duplicate category names.

// Fetch Community instances in the database, and add them to an NSMutableArray
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *community = [NSEntityDescription entityForName:@"Community" inManagedObjectContext:managedObjectContext];
[request setEntity:community];  

// Only return the community instances that have the cityName of the cell tapped in the CommunitiesNonEditableTableViewController
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(cityName like %@)", cellCityName];
[request setPredicate:predicate];       

NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

if (mutableFetchResults == nil) {
    // Handle the error.
}

// Set communitiesArray with mutableFetchResults
[self setCommunitiesArray:mutableFetchResults];

[mutableFetchResults release];
[request release];  

// Creates a community instance using the community stored in the array at index 0.  This is the only community in the array.
Community *communityInstance;
communityInstance = [communitiesArray objectAtIndex:0];

// Retrieves existing categories of assets in the community, and adds them to an NSSet
NSSet *communityCategoriesSet = communityInstance.categories;   

// Converts NSSet to an NSArray with each category as an index
NSArray *communityCategoriesArray = [communityCategoriesSet allObjects];

// For loop that iterates through the array full of categories, retrieves the names of each category, and adds it to an NSMutableArray
categoryNames = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < [communityCategoriesArray count]; i++) {
    Category *categoryInstance;
    categoryInstance = [communityCategoriesArray objectAtIndex:i];
    [categoryNames addObject:categoryInstance.name];
}   

// Prints array full of category names to console
NSLog(@"%@", categoryNames);

When I execute this, I get duplicate names in the console. Why?

+1  A: 

Uniquing means that each object in the object graph is itself unique. It does not mean that the attributes of any two objects are not identical. Uniquing is about relationship not attributes. No two objects can occupy the exact same position in the object graph.

As to why you get multiple categories in the output: The simplest explanation is that communityInstance.categories is a to-many relationship. (Since it has a plural name and you assign it to set.) In a to-many relationship, the context does not force a single object on the other end of the relationship.

TechZen
Thanks. I went about this a completely different way.
Brian Reading