我正在嘗試使用背景Threds在iPad上進行一些計算。 即使你正在運行計算器,也是如此。用戶界面在運行時被阻止... 我在做什麼錯誤。iPad阻止用戶界面的背景計算
[mc evaluateFormula:adapted runNo:10000];
這是從IBAction中調用的。
這是代碼被稱爲:
-(void)evaluateFormula:(NSDictionary *)frm runNo:(NSUInteger)runCount
{
self.runCount = runCount;
self.frm = frm;
[self performSelectorInBackground:@selector(backgroundEvalFrm) withObject:nil];
// for (int i = 0; i < runCount; i++) {
// [self runFormula:frm];
// }
//
}
-(void)backgroundEvalFrm
{
percentVal = self.runCount/100;
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:20];
for (int i = 0; i<self.runCount; i++) {
NSInvocationOperation *op =[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(runFormula:) object:self.frm];
[queue addOperation:op];
}
}
那麼,爲什麼UI受阻?
這裏是線程的返回碼......這一切都在同一個班
-(void)runFormula:(NSDictionary *)frm
{
NSMutableString *formula = [[frm objectForKey:kFormulaExpresion] mutableCopy];
NSArray *variables = [frm objectForKey:kVariableArray];
NSArray *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
//NSLog(@"formula to parse:%@",formula);
NSNumber *resNo = [formula numberByEvaluatingString];
// NSLog(@"formula %@ the result : %f",formula,[resNo doubleValue]);
//NSNumber *resNo = [NSNumber numberWithDouble:result];
[self performSelectorOnMainThread:@selector(addNewResult:) withObject:resNo waitUntilDone:NO];
}
#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];
}
}
你在做什麼runFormula – Daniel 2013-02-21 17:56:26
如果你在UI被鎖定時暫停調試器會發生什麼。你是否看到正在執行的主線程或背景上的計算? – 2013-02-21 19:16:21
runFormula我正在執行一些數學計算。 – user1028028 2013-02-22 11:29:17