2016-01-13 60 views
2

我正在处理核心数据。我有一个实体“目录”,其中有20个属性或多或少。我正在获取数据,并将谓词用作实体中属性的catalogId。在收到的数据中所有实体数据都有重复的数据,我必须避免它们。我也用这个获取过滤的数据并再次过滤Coredata中的数据

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; 

但它没有奏效。在Catalog实体中,有一个属性pageId,它在整个实体中重复。我想要使​​用catalogId的数据,其中一行具有相同的pageId应该跳过,我的意思是避免重复pageId中获取的数据.. 在此先感谢

+0

如果设置了'propertiesToFetch'并且'resultType'为'NSDictionaryResultType',那么'returnsDistinctResults'只在工作。但是,重复ID是一种气味,您的模型未被标准化。您可以将完整的提取码和您的模型的描述添加到您的Q中。 –

+0

我编辑了问题 – Aly

+0

@Aly据我所知,您的数据可能包含多个具有相同'pageId'的对象,并且您只想为每个'pageId'选择一个对象。问题是,你如何决定你想要使用哪几个对象? – pbasdf

回答

1

以下使用两个步骤的过程。使用两次提取的原因是:为每个pageid只选择一个对象,我们必须使用propertiesToGroupBy,这意味着我们必须使用NSDictionaryResultType和b)意味着我们无法获取除propertiesToGroupBy中指定的属性之外的任何属性在你之前的一个问题中找到)。这两个步骤是:

  1. 获取NSManagedObjectIDs,按pageid分组。 (虽然你不能指定其他属性,你可以指定的objectID。需要注意的是CoreData将任意选择一个对象,每个pageid,并使用它的objectID。)
  2. 获取与此的ObjectID返回NSManagedObjects由(1)。

代码以上:(检查为简洁起见省略错误)

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 "); 

+0

非常感谢@pbasdf – Aly