【问题标题】:How can i call a lot of methods in A thread?我怎样才能在一个线程中调用很多方法?
【发布时间】:2012-11-08 09:31:26
【问题描述】:

我有一个连接方法。我想在线程中使用它,因为它需要很长时间。但是这种方法会触发其他许多方法,所以我会收到类似上面的错误消息。

*_WebThreadLockFromAnyThread(bool), 0x94316f0: 从主线程或web线程以外的线程获取web lock。不应从辅助线程调用 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];
        });
    });

【问题讨论】:

标签: objective-c ios multithreading grand-central-dispatch


【解决方案1】:

UIAlertViewUIActivityIndicator 等 UIKit 组件不能在主线程以外的线程中使用。如果您想显示警报/活动,那么您必须仅在主线程中显示/关闭或启动/停止。

我认为您在不是主线程的线程中调用[spinner stopAnimating];。如果是这样,请在主线程中执行此操作。

[self performSelector:@selector(stopAnimation) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];

【讨论】:

  • 微调器没问题。当我注释掉 [weakSelf connect]; 时,我不会收到错误消息。
  • dispatch_async(dispatch_get_main_queue(), ^{ 表示主线程。
  • 然后移动你的 [weakSelf connect];进入 dispatch_async(dispatch_get_main_queue(), ^{ 并检查它是否正常工作。
  • 它在 main_queue 中工作。但我想在后台工作而不是主线程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 2020-10-27
  • 2011-07-18
  • 2021-07-14
  • 2016-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多