我有一个连接方法。我想在线程中使用它,因为它需要很长时间。但是此方法触发了其他许多方法,因此我收到了类似上面的错误消息。
* _WebThreadLockFromAnyThread(bool),0x94316f0:从除主线程或Web线程之外的其他线程获取Web锁。不应从辅助线程调用UIKit。*
我的代码是:
__weak LoginViewController *weakSelf = self;
dispatch_queue_t connectionQueue = dispatch_queue_create("connection Queue", NULL);
dispatch_async(connectionQueue, ^{
[weakSelf connect];
dispatch_async(dispatch_get_main_queue(), ^{
[spinner stopAnimating];
});
});
最佳答案
UIKit组件(如UIAlertView
,UIActivityIndicator
)不能在除主线程之外的其他线程中使用。如果要显示警报/ Activity ,则只能在主线程中显示/关闭或启动/停止。
我认为您在不是主线程的线程中调用[spinner stopAnimating];
。如果是这样,则在主线程中执行此操作。
[self performSelector:@selector(stopAnimation) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
关于objective-c - 如何在A线程中调用很多方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13286197/