Error message that UIKit should not be called from a secondary thread(不应从辅助线程调用 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 的错误消息
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01