特定线程完成其工作后如何执行一些代码?

时间:2012-06-22 14:57:46

标签: ios multithreading cocoa

我有这样的代码

- (IBAction)onClick:(id)sender {

    NSThread *thread = [[NSThread alloc]initWithTarget:parser selector:@selector(adapter:) object:ph];

    [thread start];
}

我通过线程执行选择器之后如何做某事(显示日志或smth)?

1 个答案:

答案 0 :(得分:2)

不幸的是,如果没有使用GCD(这是你可能无法支持的iOS 4.x功能),这个问题就没有简单的答案了,所以使用NSNotificationCenter并锁定通知时方法已经完成。

- (IBAction)onClick:(id)sender {

    NSThread *thread = [[NSThread alloc]initWithTarget:parser selector:@selector(adapter:) object:ph];

    [thread start];
}

-(void)adapter:(NSObject*)arg {
    //... process and such
    [[NSNotificationCenter defaultCenter]postNotification:[NSNotification notificationWithName:@"Thread_Done" object:nil]];

}