【问题标题】:Fetch the filtered data and again filter the data in Coredata获取过滤后的数据,再次过滤Coredata中的数据
【发布时间】: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 并且resultTypeNSDictionaryResultType 时才有效。但是,具有重复 ID 是您的模型未标准化的气味。您能否将完整的获取代码和模型描述添加到您的 Q 中。
  • 我已经编辑了问题
  • @Aly 据我了解,您的数据可能包含多个具有相同pageId 的对象,并且您只想为每个pageId 选择一个对象。问题是,您如何决定要使用这几个对象中的哪一个?
  • @pbasdf 您不必决定这一点,因为唯一性仅在 所有 获取的属性相等的情况下才有效。
  • @AminNegm-Awad 我认为这就是问题所在 - returnsDistinctResults on (pageId, categoryId) 将为每个 pageId 提供 categoryId 的所有值 - 据我所知(@Aly 纠正我,如果我错误)他只希望每个 pageId 有一个 categoryId 值。

标签: ios objective-c core-data nspredicate nsfetchrequest


【解决方案1】:

以下使用两步过程。使用两次提取的原因是:要为每个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 ");

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 2022-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    相关资源
    最近更新 更多