2011-02-08 97 views
0

亲爱的社区。 我有一些问题,我必须将核心数据中的数字与范围缩小进行比较。优化核心数据获取请求

这里是代码的第一部分:

 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,但我不明白我怎么可以在我的情况下使用它 PS重要的注意事项 - 我使用一个物体,我发现,我n下一步操作和父对象。

回答

1

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

+0

你能否在我的任务中提出有用的例子。 – Alex 2011-04-01 10:15:32