2010-11-16 44 views
0

我在寫一個非常簡單的OSX應用程序。在這個應用程序中,有一個主線程不斷更新一些東西,然後計算一些統計信息並在textView上打印它們。執行在[textView insertText:]上毫無例外地停止!

在開發過程中,我使用了相同的接收IBAction來執行此循環。我得到了所有的工作,然後切換到NSThread,以防止計算時鎖定用戶界面。

只要我這樣做,應用程序開始運行很少的週期(約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]; 
} 

附: 對不起,如果英文不完美,我是意大利語:)

回答

2

您的應用程序崩潰,因爲您正在從後臺線程訪問textView。 UI對象只能從主線程訪問。

要解決此問題,您需要將textview更新轉發給主UI線程。您可以使用-performSelectorOnMainThread:方法執行此操作。例如:

[textView performSelectorOnMainThread:@selector(insertText:) 
          withObject:toPrint 
         waitUntilDone:YES]; 
+0

非常感謝,工作!我不知道..我可以問爲什麼我只能從主線程訪問UI對象? – XelharK 2010-11-16 09:00:31

+0

這是因爲UI框架不是線程安全的。它們通常不是由於確保線程安全所涉及的複雜性。 – willcodejavaforfood 2010-11-16 09:02:28