[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进行操作
解决的方法是使用performSelectorOnMainThread进行ui的更新:
[self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:NO];
本文探讨了在iOS应用开发中遇到的多线程操作与UI更新的常见问题,特别是尝试在非主线程中操作UI时可能出现的崩溃错误。通过分析原因,文章详细介绍了如何使用`performSelectorOnMainThread`方法来确保在正确线程中进行UI更新,避免了应用崩溃,保障了用户体验。
9681

被折叠的 条评论
为什么被折叠?



