I am working on core data. I have an entity "Catalog", which have 20 attributes more or less. I am fetching the data and used predicate against catalogId which is the attribute in the entity. In received data all entity data but there is repeated data, I have to avoid them. I also used this
NSManagedObjectContext *context = [(CategoriesAppDelegate*)[UIApplication sharedApplication].delegate managedObjectContext];
NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@"Tbl_catalogPage"];
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Tbl_catalogPage"inManagedObjectContext:context];
[fetch setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"catalogid == '%@'", catalogId]];
[fetch setPredicate:predicate];
[fetch setPropertiesToFetch:[NSArray arrayWithObjects:[[entity attributesByName]objectForKey:@"pageid"], [[entity attributesByName]objectForKey:@"catalogid"], nil]];
[fetch setResultType:NSDictionaryResultType];
[fetch setReturnsDistinctResults : YES];
NSError* error = nil;
self.resultPageArray = [context executeFetchRequest:fetch error:&error];
NSLog(@"result array count %lu",(unsigned long)self.resultPageArray.count);
NSLog (@"names: %@",self.resultPageArray);
NSLog(@"result array values ");
return resultPageArray;
But it didn't work. In Catalog entity, there is an attribute pageId, which is repeating in the whole entity. I want the data using catalogId in which a row having the same pageId should skip, I mean avoid duplication of pageId in fetched data.. Thanks in advance
The following uses a two-step process. The reason for using two fetches is this: to select only one object for each pageid
, we must use propertiesToGroupBy
, which a) means we must use NSDictionaryResultType
and b) means we cannot fetch any attributes other than those specified in the propertiesToGroupBy
(as you found in one of your previous questions). The two steps are:
pageid
. (Although you can't specify other attributes, you can specify the objectID. Note that CoreData will arbitrarily pick one object with each pageid
, and use its objectID.)Code for the above:
NSManagedObjectContext *context = [(CategoriesAppDelegate*)[UIApplication sharedApplication].delegate managedObjectContext];
// First fetch
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Tbl_catalogPage"];
// fetch only object IDs
NSExpressionDescription *objIdED = [NSExpressionDescription new];
objIdED.expression = [NSExpression expressionForEvaluatedObject];
objIdED.name = @"objId";
objIdED.expressionResultType = NSObjectIDAttributeType;
[fetch setPropertiesToFetch:@[objIdED]];
// Group by "pageid"
[fetch setPropertiesToGroupBy:@[@"pageid"]];
// Dictionary result type required for Group By:
[fetch setResultType:NSDictionaryResultType];
NSError* error = nil;
NSArray *interimResults = [context executeFetchRequest:fetch error:&error];
// Convert the array of dictionaries into an array of NSManagedObjectIDs
NSArray *requiredObjectIDs = [interimResults valueForKey:@"objId"];
// Second fetch
NSFetchRequest *secondFetch = [NSFetchRequest fetchRequestWithEntityName:@"Tbl_catalogPage"];
// Fetch only the objects with the required objectIDs
secondFetch.predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", requiredObjectIDs];
self.resultPageArray = [context executeFetchRequest:secondFetch error:&error];
NSLog(@"result array count %lu",(unsigned long)self.resultPageArray.count);
NSLog (@"names: %@",self.resultPageArray);
NSLog(@"result array values ");
(Error checking omitted for brevity).