- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
这个函数在很多代码中都看到,一直没弄明白wait参数的含义,今天在网上找了些资料,并自己写了个demo,终于理解了。
wait是一个BOOL值,当为YES时,表示同步操作,也即当前的线程必须等到该函数执行结束才可以执行下一个操作,当为NO时,表示异步操作。例子如下:
[NSThread detachNewThreadSelector:@selector(doSomethingInBackground:) toTarget:self withObject:nil];
- (void)callBack:(id)param
{
NSLog(@"callBack");
}
- (void)doSomethingInBackground:(id)param
{
NSLog(@"before");
[self performSelectorOnMainThread:@selector(callBack:) withObject:nil waitUntilDone:wait];
NSLog(@"after");
}
当wait为NO时,输出顺序为before
after
callBack
当wait为YES时,输出顺序为
before
callBack
after