优化核心数据fetchrequest

时间:2011-02-08 21:44:52

标签: cocoa core-data

亲爱的社区。 我有一些问题,我必须将核心数据中的数字与减少的范围进行比较。

这是代码的第一部分:

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

如果我与第一个谓词不匹配,我必须删除代码卷的最后一位数(它是一个数字并再次发送获取请求。

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

不幸的是,这是获得时间和处理器资源。 一些最新的WWDC建议建议我使用@count,但我不明白如何在我的情况下使用它。 附:重要提示 - 我在下一个操作和父对象中使用了一个对象。

1 个答案:

答案 0 :(得分:1)

尝试使用子查询缩小搜索范围。Subquery NSExpression