gpt4 book ai didi

cocoa - 优化核心数据获取请求

转载 作者:行者123 更新时间:2023-12-03 18:02:28 25 4
gpt4 key购买 nike

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

这是代码的第一部分:

        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,但我不明白如何在我的情况下使用它。附注重要说明 - 我正在使用一个我在接下来的操作和父对象中找到的对象。

最佳答案

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

关于cocoa - 优化核心数据获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4938841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com