Sean
Sean

Reputation: 440

Multiple fetch requests with complex predicates

I have a couple of views which present data from a CoreData entity in my app. To retrieve the data required for the views I often have to implement multiple fetchRequests which feels wrong - perhaps I'm still making basic mistakes like thinking of CoreData in too much of a SQL database sense.

In fact for one of my views I have 22 fetch requests which may well be the correct way of achieving what I need but as an iPhone/Objective-C novice I can't help questioning my approach. Here is a snippet of my code showing 2 of many fetchRequest, could you give me a nudge in the right direction if I am doing it wrong?

      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];
        }

Thanks in advance for any assistance, links or direction you may be able to provide...

Upvotes: 0

Views: 950

Answers (1)

Scott Corscadden
Scott Corscadden

Reputation: 2860

The predicate stuff doesn't look too bad - but may I ask why you're limiting to all those NSExpressionDescriptions? Did you analyze and find that by limiting to just those properties you found concrete performance improvements? Core Data is usually pretty good at faulting/optimizing your memory, and that might clean up the statements quite a bit.

Upvotes: 0

Related Questions