I want to know why I can’t run the button action in secondary thread(see below the secondary thread code), when I do so the APP gets crashed and shows me the below error message.
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
But when I change the thread to main thread(see below main thread code) I don’t see any crash and my APP runs perfectly. Can anyone explain what is the difference, why I cannot run the action secondary thread.
[self performSelectorOnMainThread:@selector(insertdata) withObject:nil waitUntilDone:NO];
Thanks.
The error messages told you the exact reason:
If the operation doesn’t take long, there’s no point in executing it on a background thread. If the operation does take long, you should separate the data retrieval (or whatever it is that takes long) from the UI update, run the data retrieval in background and then return back to the main thread to update the UI. Preferably using GCD: