我正在编写一个非常简单的OSX应用程序。在这个应用程序中,有一个主线程不断更新一些东西,然后计算一些统计信息并在textView上打印它们。
在开发过程中,我使用相同的IBAction来执行此循环。我把它全部工作了,然后切换到NSThread以防止UI在计算时锁定。
一旦我这样做,应用程序开始运行很少的周期(大约7-8),然后整个应用程序冻结,没有任何异常。通过调试,我注意到它在尝试打印textView上的统计信息时冻结,我完全不知道如何解决这个问题。如果不在线程内,它可以工作...... 有人可以帮忙吗?代码如下。在此先感谢:)
-(IBAction) Evolve:(id)sender{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSThread detachNewThreadSelector:@selector(Evolve) toTarget:self withObject:nil];
[pool drain];
}
这就是整个周期
-(void) Evolve{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
srandom(time(NULL));
//Tag 0: Minimo, Tag 1: Massimo
int tagMinMax = [MinMax selectedTag];
//Tag 0: Rimpiazza sempre, Tag 1: Rimpiazza solo se migliori
int tagRimpiazzo = [[Rimpiazzo selectedItem] tag];
int PopNum = [tf_popNum intValue];
int maxGen = [tf_maxGen intValue];
int target = [tf_targetVal intValue];
int chromosomeLength = [tf_chromosomeLength intValue];
Environment *env = [[Environment alloc] init];
NSMutableArray *pop = [[NSMutableArray alloc] init];
for (int i = 0; i < PopNum; i++) {
[pop addObject:[[Individual alloc] initWithRandomGenesWithChromosomeLength:chromosomeLength]];
}
[env setPopulation:pop];
[pop release];
BOOL earlyBestFound = NO;
Individual *earlyBest = nil;
int i=0;
float best, avg;
while (i<maxGen && !earlyBestFound) {
NSLog(@"while");
NSArray *parents = [env selectParents];
NSLog(@"parents selected");
NSMutableArray *offspring = [[NSMutableArray alloc] init];
for (int i = 0; i < [parents count]; i+=2) {
if (i+1<[parents count]) {
NSLog(@"beginning SEX");
Individual *parent1 = [parents objectAtIndex:i];
Individual *parent2 = [parents objectAtIndex:i+1];
NSArray *children = [parent1 kCrossOverWithOtherIndividual:1 individual:parent2];
Individual *child1 = [children objectAtIndex:0];
Individual *child2 = [children objectAtIndex:1];
NSLog(@"children born");
if (tagRimpiazzo!=0) {
if (([child1 fitness] > [parent1 fitness] && tagMinMax == 0)||([child1 fitness] < [parent1 fitness] && tagMinMax == 1)) {
child1 = parent1;
}
if (([child2 fitness] > [parent2 fitness] && tagMinMax == 0)||([child2 fitness] < [parent2 fitness] && tagMinMax == 1)) {
child2 = parent2;
}
}
NSLog(@"Replacement happened");
[offspring addObject:child1];
[offspring addObject:child2];
}
}
NSLog(@"Calculating statistics");
avg = 0;
for(Individual *X in offspring){
if (([X fitness] > best && tagMinMax == 1)||([X fitness] < best && tagMinMax == 0)) {
best = [X fitness];
}
avg += [X fitness];
if ([X fitness]==target) {
earlyBestFound = YES;
earlyBest = X;
}
}
avg = avg/(float)PopNum;
[env setPopulation:offspring];
NSLog(@"Releasing some memory");
[offspring release];
NSLog(@"Printing statistics");
NSString *toPrint = [NSString stringWithFormat:@"Fine generazione: %d avg: %.2f best: %.2f \r\n", i,avg,best];
[textView insertText:toPrint];
i++;
}
NSLog(@"Fine");
NSString *toPrint = [NSString stringWithFormat:@"Fine! best: %.2f - avg: %.2f \r\n", i,avg,best];
[textView insertText:toPrint];
[env release];
[pool drain];
}
P.S。 对不起,如果英语不完美,我是意大利人:)
答案 0 :(得分:2)
您的应用程序崩溃了,因为您正在从后台线程访问textView
。 UI对象只能从主线程访问。
要解决此问题,您需要将textview更新转发到主UI线程。您可以使用-performSelectorOnMainThread:
方法执行此操作。例如:
[textView performSelectorOnMainThread:@selector(insertText:)
withObject:toPrint
waitUntilDone:YES];