I’m getting an error when trying to execute the performSegueWithIdentifier method inside a setCompletionBlock of an ASIHTTPRequest request.
Here’s some code (certain portion were ommitted):
// Instantiate request object
ASIFormDataRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://[...]"]];
// Set request headers
[request addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"];
// Format JSON request
NSString *json = [...]
// Set the post data
[request setPostBody:[[NSMutableData alloc] initWithData:[json dataUsingEncoding:NSASCIIStringEncoding]]];
__weak ASIFormDataRequest *_request = request;
// Handle success
[request setCompletionBlock:^{
// Get the response
NSDictionary *response = [[_request responseString] JSONValue];
// Do some stuff with the response...
// Show the title list
[self performSegueWithIdentifier:@"ShowTitles" sender:self];
}];
[request startAsynchronous];
The exact error I’m getting is:
bool _WebTryThreadLock(bool), 0x7d70520: 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 -[UITextRangeImpl isEmpty]
3 -[UITextRange(UITextSelectionAdditions) _isCaret]
4 -[UITextSelectionView setCaretBlinks:]
5 -[UIKeyboardImpl setCaretBlinks:]
6 -[UIKeyboardImpl setDelegate:force:]
7 -[UIKeyboardImpl setDelegate:]
8 -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:]
9 -[UIResponder _finishResignFirstResponder]
10 -[UIResponder resignFirstResponder]
11 -[UITextField resignFirstResponder]
12 -[UIView(UITextField) endEditing:]
13 -[UIWindowController _prepareKeyboardForTransition:fromView:]
14 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:]
15 -[UIViewController presentViewController:withTransition:completion:]
16 -[UIViewController presentViewController:animated:completion:]
17 -[UIViewController presentModalViewController:animated:]
18 -[UIStoryboardModalSegue perform]
19 -[UIStoryboardSegueTemplate perform:]
20 -[UIViewController performSegueWithIdentifier:sender:]
21 -[BaseLoginViewController viewTitleList]
22 __39-[BaseLoginViewController getTitleList]_block_invoke_0
23 -[ASIHTTPRequest handleStreamComplete]
24 -[ASIHTTPRequest handleNetworkEvent:]
25 _signalEventSync
26 _cfstream_shared_signalEventSync
27 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
28 __CFRunLoopDoSources0
29 __CFRunLoopRun
30 CFRunLoopRunSpecific
31 CFRunLoopRun
From that error I’m assuming it has something to do with the fact that I’m trying to go to a new view controller while I’m still using the web thread or something. I’m still new to iOS dev so I’m not too sure. Any help would really be appreciated.
Indeed, you modify the UI from thread other than the UI (main) thread here:
You need to execute it on the main thread. You can do it using
performSelectorOnMainThread.