我在 a 中运行一个数学公式NSOperation
:
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:20];
for (int i = 0; i<runCount; i++) {
NSInvocationOperation *op =[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(runFormula:) object:frm];
[queue addOperation:op];
}
这是方法体:
-(void)runFormula:(NSDictionary *)frm
{
NSMutableString *formula = [[frm objectForKey:kFormulaExpresion] mutableCopy];
NSArray *variables = [frm objectForKey:kVariableArray];
evals = [self evaluateVariables:variables];
for (NSDictionary *var in evals) {
NSString *sym = [var objectForKey:kVariableSymbol];
[formula replaceOccurrencesOfString:sym withString:[[var objectForKey:@"numVal"] stringValue] options:NSCaseInsensitiveSearch range:NSMakeRange(0, [formula length])];
}
//parse formula
double result = [formula evaluateMath];
NSLog(@"formula %@ the result : %f",formula,result);
NSNumber *resNo = [NSNumber numberWithDouble:result];
[self performSelectorOnMainThread:@selector(addNewResult:) withObject:resNo waitUntilDone:NO];
}
问题是,如果我将 runCount 设为 100,我只会收到大约 96 个结果……这是为什么呢?还有一件事:第一个结果是胡说八道,但其他94-95都可以。
这是数据聚合方法...
#pragma mark -- data aggregation delegate
-(void)addNewResult:(NSNumber *)nr
{
NSLog(@"index : %i result: %f",currentIndex,[nr doubleValue]);
[[self delegate] didReceiveResult:nr];
resultsArray[currentIndex]=[nr doubleValue];
currentIndex ++;
if ( (currentIndex % percentVal) == 0) {
[[self delegate] percentCompleted];
}
}
问题可能是这与并发方法在同一个类中吗?