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 的错误消息
- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01