gpt4 book ai didi

objective-c - 错误消息,不应从辅助线程调用UIKit

转载 作者:行者123 更新时间:2023-12-03 13:14:14 25 4
gpt4 key购买 nike

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

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

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



我完全不知道如何解决此问题。

这是代码;
- (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];
}

最佳答案

"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。

假设 parseDidComplete是消息的来源,那么类似的事情将“起作用”:
[delegate performSelectorOnMainThread: @selector(parseDidComplete) withObject: nil waitUntilDone: YES];

“工作”是因为线程很难,并且此答案完全忽略了您可能遇到的任何同步问题。

请注意,最好使用 NSOperationNSOperationQueue。它们有据可查,并且有很多示例。

关于objective-c - 错误消息,不应从辅助线程调用UIKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4672254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com