Warning about obtaining lock from other thread when accessing text field

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 05:49:02

问题


I have a UITextField named _passwordTextField, where I'm hiding the input:

[_passwordTextField  setSecureTextEntry:YES]; 

every time I access the field's text with passwordTextField.text, I see this warning:

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

How do I remove this warning?


回答1:


This has nothing to do with UITextField. iOS UIKit requires any direct manipulation to UI to be done on main thread & not from background thread.

If you are doing this, your app will be unstable and in some situations might even crash. So as a rule do not update UIKit elements from background thread. Please inspect your code.

If you're using iOS >= 4, you can use GCD do this instead:

dispatch_async(dispatch_get_main_queue(), ^{
    [self doSomething:1 b:2 c:3 d:4 e:5]; //changes UI method
});

UPDATE: please check your code again. when dealing with threading issues, its not which line this warning occurs you need to observe but you need to see in its entirety. See if the entire method is getting called from main thread or background thread..



来源:https://stackoverflow.com/questions/14207483/warning-about-obtaining-lock-from-other-thread-when-accessing-text-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!