核心数据 - NSPredicate具有相同列的对象,也符合另一个条件

时间:2015-11-11 21:09:27

标签: core-data nspredicate

我正在寻找一个谓词来获取Entity类型的所有托管对象,其值在属性sessionId中重复,其中所有组都是' (" groups",表示sessionId相等的托管对象)内容'属性processed中的标志设置为YES。这可以(慢慢地)完成,但我正在为此寻找一个有效的一个班轮。感谢

这是缓慢的方式:

NSFetchRequest *request = [NSEntityDescription entityForName:@"Entity"
                                      inManagedObjectContext:context];
NSArray *all = [context executeFetchRequest:request error:nil];
NSArray *sessionIds = [all valueForKeyPath:@"@distinctUnionOfObjects.sessionId"];
NSMutableArray *objects = [NSMutableArray array];
for (NSString *sessionId in sessionIds) {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sessionId == %@", sessionId];
    NSArray *inSession = [all filteredArrayUsingPredicate:predicate];
    for(id obj in inSession) {
         if(![obj valueForKey:@"processed"]) continue;
    }

    [objects arrayByAddingObjectsFromArray:processed];
}
NSLog(@"%@", objects);

1 个答案:

答案 0 :(得分:1)

由于布尔值存储为0和1,因此所有行都为processed = YES的组将具有average(processed) = 1。因此,您可以使用NSFetchRequest propertiesToGroupByhavingPredicate来获取符合条件的sessionId。然后需要第二次获取以获取EntitysessionId对象中的NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Entity"]; fetch.resultType = NSDictionaryResultType; fetch.propertiesToFetch = @[@"sessionId"]; fetch.propertiesToGroupBy = @[@"sessionId"]; fetch.havingPredicate = [NSPredicate predicateWithFormat: @"average:(processed) == 1"]; NSArray *resultsArray = [context executeFetchRequest:fetch error:nil]; NSArray *sessionIdArray = [resultsArray valueForKeyPath:@"sessionId"]; NSFetchRequest *newFetch = [NSFetchRequest fetchRequestWithEntityName:@"Entity"]; newFetch.predicate = [NSPredicate predicateWithFormat:@"name IN %@",sessionIdArray]; NSArray *finalResults = [context executeFetchRequest:newFetch error:nil]; NSLog(@"Final results, %@", finalResults); 个对象:

NSFetchRequestExpression

对不起它不是单行。我留给你,以确定它是否比你自己的代码更快。

修改

要在一次抓取中完成所有操作,请使用NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Entity"]; fetch.resultType = NSDictionaryResultType; fetch.propertiesToFetch = @[@"sessionId"]; fetch.propertiesToGroupBy = @[@"sessionId"]; fetch.havingPredicate = [NSPredicate predicateWithFormat: @"average:(processed) == 1"]; NSExpression *fetchExpression = [NSFetchRequestExpression expressionForFetch:[NSExpression expressionForConstantValue:fetch] context:[NSExpression expressionForConstantValue:context] countOnly:false]; NSFetchRequest *newFetch = [NSFetchRequest fetchRequestWithEntityName:@"Entity"]; newFetch.predicate = [NSPredicate predicateWithFormat:@"sessionId IN %@",fetchExpression]; NSArray *finalResults = [context executeFetchRequest:newFetch error:nil]; NSLog(@"Final results, %@", finalResults); 代替中间数组:

- (void) addImageViewToScrollView {

    //create ArrayImages
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];

    for (int x = 0; x < 170; x++) {

        [tempArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",x]]];

    }
    self.imagesArray = [NSArray arrayWithArray:tempArray];

    //Frame ScrollView in UIView
    self.scrollView.contentSize =
    CGSizeMake(self.scrollView.frame.size.width * self.imagesArray.count,
           self.scrollView.frame.size.height);

    //scrollView add subview ImageView
    for (int i = 0; i < self.imagesArray.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;
        _imageView = [[UIImageView alloc] init];
        _imageView.image = [self.imagesArray objectAtIndex:i];
        _imageView.frame = frame;
        [scrollView addSubview:_imageView];

    }
    self.pageControll.currentPage = 0;
    self.pageControll.numberOfPages = self.imagesArray.count;
}

请注意,在我的(通常是微不足道的)测试设置中,这实际上比双获取解决方案运行得慢。

仅供参考,如果您使用SQLDebug构建设置来检查生成的SQL,它看起来像这样:

  

SELECT 0,t0.Z_PK,t0.Z_OPT,t0.ZSESSIONID,t0.ZURROCESSED FROM ZENTITY t0 WHERE t0.ZSESSIONID IN(SELECT n1_t0.ZSESSIONID FROM ZENTITY n1_t0 GROUP BY n1_t0.ZSESSIONID,avg(n1_t0.ZPROCESSED)= ?)