具有复杂谓词的多个提取请求

时间:2012-03-19 10:04:43

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

我有几个视图,它们在我的应用程序中显示来自CoreData实体的数据。要检索视图所需的数据,我经常需要实现多个感觉错误的fetchRequests - 也许我仍然会犯一些基本的错误,就像在SQL数据库意义上过多考虑CoreData一样。

事实上,对于我的一个观点,我有22个获取请求,这可能是实现我需要的正确方法,但作为iPhone / Objective-C新手,我不禁质疑我的方法。这是我的代码片段,显示了许多fetchRequest中的两个,如果我做错了,你能否给我一个正确方向的推动?

      SGK_T4T_01AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSEntityDescription *entityDiscription = [NSEntityDescription entityForName:@"Sessions" inManagedObjectContext:context];

//Swim 3 Count
        NSFetchRequest *request2 = [[NSFetchRequest alloc] init];
        [request2 setEntity:entityDiscription];
        [request2 setResultType:NSDictionaryResultType];
        NSExpression *keyPathExpression2 = [NSExpression expressionForKeyPath:@"sport"];
        NSExpression *swimCountExpression = [NSExpression expressionForFunction:@"count:" arguments:[NSArray arrayWithObject:keyPathExpression2]];
        NSExpressionDescription *expressionDescription2 = [[NSExpressionDescription alloc] init];
        [expressionDescription2 setName:@"swimCount"];
        [expressionDescription2 setExpression:swimCountExpression];
        [expressionDescription2 setExpressionResultType:NSInteger16AttributeType];
        [request2 setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription2]];
        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"(date >= %@ AND sport like %@)", swimSinceDateAsDate, sportTypeSwim];
        [request2 setPredicate:pred2];

        NSError *error2;
        NSArray *objects2 = [context executeFetchRequest:request2 error:&error2];
        if (objects2 == nil) {
            NSLog(@"The fetch request returned an array == nil");
        } else {
            _swimTotalSwimCountLabel.text = [[NSString alloc] initWithFormat:@"%@", [[objects2 objectAtIndex:0] valueForKey:@"swimCount"]];
        }

        //Swim 4 Fastest 1500m Time Trial
        NSFetchRequest *request3 = [[NSFetchRequest alloc] init];
        [request3 setEntity:entityDiscription];
        [request3 setResultType:NSDictionaryResultType];
        NSExpression *keyPathExpression3 = [NSExpression expressionForKeyPath:@"time1"];
        NSExpression *swimfastest1500Expression = [NSExpression expressionForFunction:@"min:" arguments:[NSArray arrayWithObject:keyPathExpression3]];
        NSExpressionDescription *expressionDescription3 = [[NSExpressionDescription alloc] init];
        [expressionDescription3 setName:@"swimFastest1500"];
        [expressionDescription3 setExpression:swimfastest1500Expression];
        [expressionDescription3 setExpressionResultType:NSInteger16AttributeType];
        [request3 setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription3]];
        NSString *sessType3 = @"Time Trial - 1500m";
        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"(date >= %@ AND sport like %@ AND sessiontype like %@)", swimSinceDateAsDate, sportTypeSwim, sessType3];
        [request3 setPredicate:pred3];

        NSError *error3;
        NSArray *objects3 = [context executeFetchRequest:request3 error:&error3];
        if (objects3 == nil) {
            NSLog(@"The fetch request returned an array == nil");
        } else {
            NSUInteger durationInSeconds = [[[objects3 objectAtIndex:0] valueForKey:@"swimFastest1500"] integerValue];
            NSUInteger durationInMinutes = durationInSeconds / 60;
            NSUInteger durationRemainder = durationInSeconds % 60;
            _swimTt1500PaceLabel.text = [[NSString alloc] initWithFormat:@"%02i:%02i", durationInMinutes, durationRemainder];
        }

提前感谢您提供的任何帮助,链接或指示......

1 个答案:

答案 0 :(得分:0)

谓词的东西看起来并不太糟糕 - 但是我可以问你为什么要限制所有那些NSExpressionDescriptions?您是否通过限制这些属性进行分析并发现具体的性能改进?核心数据通常非常适合于错误/优化内存,这可能会对语句进行相当多的清理。