尝试在ASIHTTPRequest请求的setCompletionBlock中执行performSegueWithIdentifier方法时出错。
这是一些代码(某些部分被省略):
// 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];
我得到的确切错误是:
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
从那个错误我认为它与我正在尝试去一个新的视图控制器这一事实有关,而我仍在使用网络线程或其他东西。我还是iOS开发新手,所以我不太确定。任何帮助都会非常感激。
答案 0 :(得分:1)
实际上,您可以从此处的UI(主)线程以外的线程修改UI:
[self performSegueWithIdentifier:@"ShowTitles" sender:self];
您需要在主线程上执行它。您可以使用performSelectorOnMainThread
。