[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
会创建一个新的线程实行fetchedData函数,并传入参数data,并且会等待函数退出后再继续执行。
- (void)fetchedData:(NSData *)responseData {
。。。}
在多线程操作中,有一个著名的错误,叫做“Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread”,一旦出现这个错误,程序会立即crashed。
这是由于,apple不允许程序员在主线程以外的线程中对ui进行操作(Bug?)
而笔者在一次http异步操作中也出现过这个错误。当时使用了NSOperation进行了http异步请求,然后使用kvo模式注册观察者,当数据下载完毕后,在主线程中接收下载完毕的通知,并在observeValueForKeyPath方法中使用[tableview reloadData]更新UI。
这样也导致了上述错误。
解决的方法是使用performSelectorOnMainThread进行ui的更新:
[self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:NO];
iphone开发中,在主线程之外的线程更新主线程所显示的界面元素,不能直接调用主线程的类的更新界面的方法,否则界面看不到任何结果
.而是需要有个缓冲,叫做
performSelectorOnMainThread
使用方法是,在线程调用到更新界面那个类的时候,在更新界面前,通过如下调用.
[self performSelectorOnMainThread:@selector(updateLabel:) withObject:updateData waitUntilDone:YES];
比如说你已经定义了一个- (void)updateLabel:(NSString *)txt{}的方法.在此方法中更新界面即可.即时看到结果。
注意点
1:
发现一个问题,就是如果从另外一个线程向主线程传对象updateData,如果你这个updateData在调用performSelectorOnMainThread之后马上释放的话
到真正@selector那个方法取updateData的时候已经不可用了.建议使用lock机制访问资源。
2:如果主线程忙于其他的事情,子线程即使调用performSelectorOnMainThread更新了也不会更新在界面上。
http://blog.youkuaiyun.com/ericsuper/article/details/6998870
http://blog.youkuaiyun.com/jiantiantian/article/details/13622821