【发布时间】:2011-11-25 06:49:06
【问题描述】:
我想知道为什么我不能在辅助线程中运行按钮操作(请参阅下面的辅助线程代码),当我这样做时,APP 崩溃并显示以下错误消息。
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(insertdata) object:nil];
[thread start];
void _WebThreadLockFromAnyThread(bool), 0x6a8cfe0: Obtaining the web lock from a thread other than the main thread or the web thread. UIKit should not be called from a secondary thread.
bool _WebTryThreadLock(bool), 0x6a8cfe0: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
1 WebThreadLock
2 -[UIFieldEditor setText:andSetCaretSelectionAfterText:]
3 -[UITextField setText:]
4 -[XYZ reset]
5 -[XYZ insertdata]
6 -[NSThread main]
7 __NSThread__main__
8 _pthread_start
9 thread_start
但是当我将线程更改为主线程时(见下面的主线程代码)我没有看到任何崩溃并且我的 APP 运行完美。谁能解释一下有什么区别,为什么我不能运行动作辅助线程。
[self performSelectorOnMainThread:@selector(insertdata) withObject:nil waitUntilDone:NO];
谢谢。
【问题讨论】:
标签: objective-c uibutton ios5 nsthread