不应从辅助线程调用 UIKit 的错误消息

Error message that UIKit should not be called from a secondary thread(不应从辅助线程调用 UIKit 的错误消息)

本文介绍了不应从辅助线程调用 UIKit 的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它使用 UISearchBar 根据用户输入从外部 API 动态搜索.

I have an app which uses a UISearchBar to dynamically search from an external API based on user input.

应用程序正在搜索外部 API 并正确显示结果,但是当我从搜索结果中选择任何行时,屏幕冻结并出现此错误;

The app is searching the external API fine and displaying results correctly, but when I select any row from the search results, the screen freezes and I am getting this error;

试图从主线程或web线程以外的线程获取web lock不应从辅助线程调用 UIKit

Tried to obtain the web lock from a thread other than the main thread or the web thread UIKit should not be called from a secondary thread

我完全不知道如何解决这个问题.

I have absolutely no idea how I can fix this.

这里是代码;

- (void) run: (id) param  {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL: [self URL]];
    [parser setDelegate: self];
    [parser parse];
    [parser release];
    [delegate parseDidComplete];
    [pool release];
} 

- (void) parseXMLFile: (NSURL *) url
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [self setURL: url];
    NSThread* myThread = [[NSThread alloc] initWithTarget:self
                                                 selector:@selector(run
   object: nil];
    [myThread start];
    [pool release];
}

推荐答案

"试图从一个主线程以外的线程或网络线程 UIKit 不应该是从辅助线程调用"

"Tried to obtain the web lock from a thread other than the main thread or the web thread UIKit should not be called from a secondary thread"

修复在概念上很简单;不要从您的线程更新 UI.

The fix is conceptually simple; don't update the UI from your thread.

假设 parseDidComplete 是消息的来源,那么这样的事情将起作用":

Assuming the parseDidComplete is where the message is sourced, then something like this will "work":

[delegate performSelectorOnMainThread: @selector(parseDidComplete) withObject: nil waitUntilDone: YES];

工作",因为线程很难,而且这个答案完全忽略了您可能遇到的任何同步问题.

"Work" because threading is hard and this answer completely ignores any synchronization issues you might have.

请注意,最好使用 NSOperation 和 NSOperationQueue.它们有据可查,并且有很多示例.

Note that you'd be better off using NSOperation and NSOperationQueue. They are well documented and there are a bunch of examples.

这篇关于不应从辅助线程调用 UIKit 的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:不应从辅助线程调用 UIKit 的错误消息