performSelectorOnMainThread的语义:withObject:waitUntilDone:

时间:2012-05-14 20:09:01

标签: iphone objective-c multithreading

我所拥有的是

- (void)startTheBackgroundJob {   
    1.NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    2.[NSThread sleepForTimeInterval:3];
    3.[self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:YES];
    4.//[self makeMyProgressBarMoving];
    5.[pool release];

}

- (void)makeMyProgressBarMoving {

    float actual = [threadProgressView progress];
    threadValueLabel.text = [NSString stringWithFormat:@"%.2f", actual];
    if (actual < 1) {
        threadProgressView.progress = actual + 0.01;
        [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];
    }
    else threadStartButton.hidden = NO;

}

在starTheBackgroundJob的第4行,您可以设置waitUntilDone = YES(或NO)的值。

我阅读了参考资料,并指出:

  

等待

     

一个布尔值,指定当前线程是否在之后阻塞   指定的选择器在主接收器上执行   线。指定YES以阻止此线程;否则,指定NO   让这个方法立即返回。        如果当前线程也是主线程,并且为此参数指定YES,则传递并处理该消息   立即

我的问题是:

  1. 这里指定的选择器是什么(在本例中)?
  2. 除了问题。设置waitUntilDone = YES时会发生什么。我尝试了但是我没有看到任何差别。

1 个答案:

答案 0 :(得分:0)

1)makeMyProgressBarMoving是你的选择器。 2)如果您尝试在另一个线程内的主线程上执行选择器,则只能使用onMainThread版本。文档说:

等待:boolen指定E当前线程是否阻塞直到在主线程上的接收器上执行指定的选择器之后。