错误消息,UIKit不应该从辅助线程调用 [英] Error message that UIKit should not be called from a secondary thread

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

问题描述

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

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

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

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锁
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];
}


推荐答案


尝试从
线程而不是主线程或
获取Web锁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,因为线程很难,这个答案完全忽略了你可能遇到的任何同步问题。

"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不应该从辅助线程调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆