Alex
Alex

Reputation: 393

optimize core-data fetchrequest

Dear Community. I have some issues, where i have to compare digits in core data with decreased range.

here is a first part of code:

        NSFetchRequest *requestDestinationWeBuy = [[[NSFetchRequest alloc] init] autorelease];
        [requestDestinationWeBuy setEntity:[NSEntityDescription entityForName:@"DestinationsListWeBuy"
                                                       inManagedObjectContext:moc]];
        NSError *error = nil; 
        [requestDestinationWeBuy setPredicate:
         [NSPredicate predicateWithFormat:
          @"carrier.name == %@ and (country == %@) and (specific == %@) and (prefix == %@) and (enabled == YES) and ((rateSheet == %@) OR (rateSheet == %@))",
          outCarrierName,
          countryStr,
          specificStr,
          outCarrierPrefixStr,
          outCarrierRateSheet,
          @"Price table"]];
        NSArray *destinationWeBuyList = nil;
        //[requestDestinationWeBuy includesSubentities];
        [requestDestinationWeBuy setResultType:NSManagedObjectIDResultType];
        destinationWeBuyList = [moc executeFetchRequest:requestDestinationWeBuy error:&error];

if i didn't match first predicate, i have to cut last digit of code volume (it's a digits and send fetch request again.

            int maxCodeDeep = 8; 
            if ([codeStr length] < maxCodeDeep) maxCodeDeep = [[NSNumber numberWithUnsignedInt:[codeStr length]] intValue] - 1;

            NSRange codeStrRange = NSMakeRange(0,[codeStr length]);
            NSString *changedCode = [NSString string];
            BOOL huntingWasSuccess = NO;
            for (NSUInteger codeDeep = 0; codeDeep < maxCodeDeep;codeDeep++) 
            {
                codeStrRange.length = codeStrRange.length - 1;
                changedCode = [codeStr substringWithRange:codeStrRange];
                NSFetchRequest *compareCode = [[[NSFetchRequest alloc] init] autorelease];
                [compareCode setEntity:[NSEntityDescription entityForName:@"CodesvsDestinationsList"
                                                   inManagedObjectContext:moc]];
                NSString *codeRelationshipName = @"destinationsListWeBuy";
                [compareCode setPredicate:[NSPredicate predicateWithFormat:@"(%K.carrier.name == %@) and ((code == %@) OR (originalCode == %@)) and (%K.prefix == %@) and (enabled == YES) and ((rateSheetID == %@) OR (rateSheetID == %@))",codeRelationshipName, outCarrierName,changedCode,changedCode, codeRelationshipName, outCarrierPrefixStr,outCarrierRateSheetID,@"65535"]];
                //[compareCode includesSubentities];
                //[compareCode includesPropertyValues];

                [compareCode setResultType:NSManagedObjectIDResultType];

                NSArray *codeAfterComparing = [moc executeFetchRequest:compareCode error:&error];
                if ([codeAfterComparing count] == 0) {
                    NSLog(@"ROUTING: Compare was unsucceseful with parameters:%@",compareCode);
                    continue;
                }
                else {
                    destinationWeBuy = [[moc objectWithID:[codeAfterComparing lastObject]] valueForKey:codeRelationshipName];
                    NSLog(@"ROUTING: Compare was succeseful with parameters:%@\n and destination object:%@\n Carrier name is %@ ",compareCode,destinationWeBuy,carrierName);

                    //destinationWeBuy = [destinationWeBuyObj objectID];
                    huntingWasSuccess = YES;
                    break;
                }
            }

Unfortunately, this is get a time and processor resources. Some of latest WWDC recommendations is propose me to using @count, but i don't understand how i can using it in my case. p.s. important note - i'm using a object, which i find, in next operations and parent object.

Upvotes: 0

Views: 583

Answers (1)

Dan
Dan

Reputation: 1729

Try using subquery to narrow down your search area.Subquery NSExpression

Upvotes: 1

Related Questions