问题
With reference this question answer
i am trying to show output string in textview but its kill it. please check
NSLog(@"viewcontroller response =%@",responseString);
txt_output.text=[NSString stringWithFormat:@"%@",strResponse];
txt_output is in my ViewcontrollerB, and i am returning from ViewcontrollerA
NSString *returnstring=[NSString stringWithFormat:@"request=%@ responstring=%@ error=%@",request,responseString,error]; completionHandler(responseString);
Resonse print on log , but after it kills while showing on textview
viewcontroller response =request=<NSMutableURLRequest: 0x786b1b30> { URL: myhosturl } responstring= error=Error Domain=NSURLErrorDomain Code=-1001 "The operation couldn’t be completed. (NSURLErrorDomain error -1001.)" UserInfo=0x786bb400 {NSErrorFailingURLKey=http://myhosturl; NSErrorFailingURLStringKey= myhosturl, NSUnderlyingError=0x7a1790d0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1001.)"}
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'
*** First throw call stack:
(
0 CoreFoundation 0x01cc5946 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x0194ea97 objc_exception_throw + 44
2 CoreFoundation 0x01cc57da +[NSException raise:format:arguments:] + 138
3 Foundation 0x015c287e -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 102
4 UIFoundation 0x03f2bfc2 -[NSLayoutManager(NSPrivate) _resizeTextViewForTextContainer:] + 418
5 UIFoundation 0x03f2bcbe -[NSLayoutManager(NSPrivate) _recalculateUsageForTextContainerAtIndex:] + 2017
6 UIFoundation 0x03f659e4 -[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] + 871
7 UIFoundation 0x03f65af4 -[NSLayoutManager processEditingForTextStorage:edited:range:changeInLength:invalidatedRange:] + 85
8 UIFoundation 0x03f8f9eb -[NSTextStorage _notifyEdited:range:changeInLength:invalidatedRange:] + 153
9 UIFoundation 0x03f8f50a -[NSTextStorage processEditing] + 458
10 UIFoundation 0x03f8f094 -[NSTextStorage endEditing] + 80
11 UIFoundation 0x03f8f11f -[NSTextStorage coordinateEditing:] + 67
12 UIKit 0x00b4631e -[UITextView setAttributedText:] + 250
13 UIKit 0x00b4c305 -[UITextView setText:] + 149
14 MYApp 0x0003fe75 __36-[TestApiViewController methodcallAPI:]_block_invoke + 197
15 MYTESTSDK 0x000d0bb5 __43-[ViewcontrollerA callAPIWithCompletionHandler:]_block_invoke + 533
16 CFNetwork 0x02650c03 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 343
17 Foundation 0x015eb715 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 12
18 Foundation 0x01512165 -[NSBlockOperation main] + 99
19 Foundation 0x014f1567 -[__NSOperationInternal _start:] + 700
20 Foundation 0x014f1299 -[NSOperation start] + 83
21 Foundation 0x014f10e3 __NSOQSchedule_f + 237
22 libdispatch.dylib 0x02e22e2f _dispatch_client_callout + 14
23 libdispatch.dylib 0x02e08afc _dispatch_queue_drain + 1475
24 libdispatch.dylib 0x02e083c3 _dispatch_queue_invoke + 212
25 libdispatch.dylib 0x02e0b067 _dispatch_root_queue_drain + 466
26 libdispatch.dylib 0x02e0c84a _dispatch_worker_thread3 + 115
27 libsystem_pthread.dylib 0x0317e296 _pthread_wqthread + 724
28 libsystem_pthread.dylib 0x0317beea start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
回答1:
You can not perform any UI related operations on background threads. Here you are trying to set text in textview on background thread. Thats why exception alerting to run it on main thread. You can update textview text on main thread as given below.
Try to set text using this code :
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"viewcontroller response =%@",responseString);
txt_output.text=[NSString stringWithFormat:@"%@",strResponse];
});
回答2:
You're trying to call UIKit methods from a thread other than the main thread, which is not allowed. The exception actually tells you this directly, in the "reason" field:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'
You should run your completion block on the main thread, so that any UIKit work the block does will happen on the main thread.
来源:https://stackoverflow.com/questions/34960599/error-showing-response-in-uitextview