【发布时间】:2018-02-15 23:03:49
【问题描述】:
在我的 iPhone 应用程序中,我需要连接到网络服务器,因为这可能需要一些时间,我正在使用这样的线程:
[NSThread detachNewThreadSelector:@selector(sendStuff) toTarget:self withObject:nil];
- (void)sendStuff {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//Need to get the string from the textField to send to server
NSString *myString = self.textField.text;
//Do some stuff here, connect to web server etc..
[pool release];
}
在我使用 self.textField 的行中,我在控制台中收到一条警告: void _WebThreadLockFromAnyThread(bool), 0x5d306b0:从主线程或web线程以外的线程获取web lock。不应从辅助线程调用 UIKit。
如何使用 textField 而不出现此错误?
【问题讨论】:
-
为了帮助您,我们需要知道为什么要分离线程(需要这么长时间才能完成)、您需要哪些 UI 信息以及要更新哪些 UI 信息。
-
嗨!我更新了问题。我需要连接到 Web 服务器,我需要使用 textField 中的文本发送到服务器。
-
您应该接受 Graham Lee 或 tonklon 的回答。您应该在生成线程之前提取搜索文本并将其作为参数传递给选择器。
标签: iphone multithreading