【发布时间】:2016-01-13 05:56:40
【问题描述】:
我正在处理核心数据。我有一个实体“目录”,它或多或少有 20 个属性。我正在获取数据并针对catalogId使用谓词,它是实体中的属性。在接收到的数据中,所有实体数据但有重复数据,我必须避免它们。我也用过这个
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.. 提前致谢
【问题讨论】:
-
returnsDistinctResults仅在设置了propertiesToFetch并且resultType为NSDictionaryResultType时才有效。但是,具有重复 ID 是您的模型未标准化的气味。您能否将完整的获取代码和模型描述添加到您的 Q 中。 -
我已经编辑了问题
-
@Aly 据我了解,您的数据可能包含多个具有相同
pageId的对象,并且您只想为每个pageId选择一个对象。问题是,您如何决定要使用这几个对象中的哪一个? -
@pbasdf 您不必决定这一点,因为唯一性仅在 所有 获取的属性相等的情况下才有效。
-
@AminNegm-Awad 我认为这就是问题所在 -
returnsDistinctResultson (pageId, categoryId) 将为每个 pageId 提供 categoryId 的所有值 - 据我所知(@Aly 纠正我,如果我错误)他只希望每个 pageId 有一个 categoryId 值。
标签: ios objective-c core-data nspredicate nsfetchrequest