performselectoronmainthread
2012-07-04 14:20
435 查看
[selfperformSelectorOnMainThread:@selector(fetchedData:) withObject:datawaitUntilDone: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];
会创建一个新的线程实行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];
相关文章推荐
- -performSelectorOnMainThread:withObject:waitUntilDone: VS dispatch_async(dispatch_get_main_queue(),
- 利用performSelectorInBackground和performSelectorOnMainThread实现多线程
- NSOperation与performSelectorOnMainThread在不同线程中更新界面
- performSelectorOnMainThread 和detachNewThreadSelector区别
- iOS: 学习笔记, 使用performSelectorOnMainThread及时刷新UIImageView
- performSelectorOnMainThread
- performSelectorOnMainThread 和detachNewThreadSelector区别.
- IOS学习--performSelectorOnMainThread
- NSOperation与performSelectorOnMainThread
- 多线程的使用(1) - performSelectorOnMainThread
- performSelectorOnMainThread
- ios更新UI时请尝试使用performSelectorOnMainThread方法
- iOS: 学习笔记, performSelectorOnMainThread
- performSelectorOnMainThread:withObject:waitUntilDone: not found in proctrol
- performSelectorOnMainThread
- NSOperation与performSelectorOnMainThread
- iOS中performSelectorOnMainThread刷新图片
- performSelectorOnMainThread 对参数的引用计数
- NSOperation与performSelectorOnMainThread
- performSelectorOnMainThread