我正在处理核心数据。我有一个实体“目录”,其中有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中获取的数据.. 在此先感谢
如果设置了'propertiesToFetch'并且'resultType'为'NSDictionaryResultType',那么'returnsDistinctResults'只在工作。但是,重复ID是一种气味,您的模型未被标准化。您可以将完整的提取码和您的模型的描述添加到您的Q中。 –
我编辑了问题 – Aly
@Aly据我所知,您的数据可能包含多个具有相同'pageId'的对象,并且您只想为每个'pageId'选择一个对象。问题是,你如何决定你想要使用哪几个对象? – pbasdf