NSThread是MainThread异常

时间:2014-04-22 01:35:13

标签: ios objective-c nsthread

有人可以向我解释这条错误消息的含义吗?

Assertion failed: ([NSThread isMainThread]), function -[AFContextManager addContextProvider:], file /SourceCache/MobileAssistantFramework/MobileAssistantFramework-651.49/AFContextManager.m, line 113.

这只发生在运行我的应用程序的1/8左右。

我相信此代码段中的最后一行代码导致了它。

HUD = [[MBProgressHUD alloc]initWithView:self.view];
HUD.labelText = @"Scanning..";
[self.view addSubview:HUD];

[HUD showWhileExecuting:@selector(scanWithImage:) onTarget:self withObject:image animated:YES];

3 个答案:

答案 0 :(得分:2)

强制它在主线程上运行。喜欢这个

dispatch_sync(dispatch_get_main_queue(), ^{
    HUD = [[MBProgressHUD alloc]initWithView:self.view];
    HUD.labelText = @"Scanning..";
    [self.view addSubview:HUD];

    [HUD showWhileExecuting:@selector(scanWithImage:) onTarget:self withObject:image animated:YES];
});

这假设UI在执行上述代码时没有阻塞,并且您绝对希望它是同步的。否则使用dispatch_async

非常安全

答案 1 :(得分:1)

addContextProvider:方法断言它正在主线程上运行。可能问题是你必须从后台线程中调用必须从主线程调用的方法。

答案 2 :(得分:1)

试试这个:

HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.labelText = @"Scanning..";
[HUD showWhileExecuting:@selector(scanWithImage:) onTarget:self withObject:image animated:YES];

希望这会有所帮助.. :)