当前位置: 动力学知识库 > 问答 > 编程问答 >

cocoa touch - Objective C, What is this warning?

问题描述:

void _WebThreadLockFromAnyThread(bool), 0x4c60190: 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.

I get this warning message if I try to get text from a UITextView.

网友答案:

Did you spawn another thread and call UIKit method from in it? Methods like + (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument or - (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg will spawn a new thread.

UI related codes should be used only in main thread. Because many of UI code uses multithreading heavily, so allowing of calling UI code from any thread easily cause unmanageable deadlock. So most of UI frameworks have this rule.

You can force a code to run in main thread by using method like - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

As an example,

- (void)UIControlCode
{
}
- (void)processingCodeInOtherThread
{
    [self performSelectorOnMainThread:@selector(UIControlCode) withObject:nil waitUntilDone:NO];
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: