取出过滤后的数据,再次过滤Coredata中的数据

Fetch the filtered data and again filter the data in Coredata

我正在处理核心数据。我有一个实体 "Catalog",它或多或少有 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 .. 提前致谢

下面使用了一个two-step过程。使用两次获取的原因是:为了 select 每个 pageid 只有一个对象,我们必须使用 propertiesToGroupBy,这 a) 意味着我们必须使用 NSDictionaryResultType 和 b)意味着我们无法获取 propertiesToGroupBy 中指定的属性以外的任何属性(正如您在之前的一个问题中发现的那样)。这两个步骤是:

  1. 获取 NSManagedObjectID,按 pageid 分组。 (虽然不能指定其他属性,但可以指定objectID。注意CoreData会任意 用每个 pageid 选择一个对象,并使用它的 objectID。)
  2. 使用 (1) 返回的 objectID 获取 NSManagedObjects。

以上代码:

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

(为简洁起见省略了错误检查)。