在UITextview中显示响应时出错

参考这个问题的答案

我正在尝试在textview中显示输出string,但它杀死它。 请检查

NSLog(@"viewcontroller response =%@",responseString); txt_output.text=[NSString stringWithFormat:@"%@",strResponse]; 

txt_output是在我的ViewcontrollerB,我从ViewcontrollerA返回

 NSString *returnstring=[NSString stringWithFormat:@"request=%@ responstring=%@ error=%@",request,responseString,error]; completionHandler(responseString); 

Resonse在日志上打印,但在文本视图上显示后死亡

 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) 

您不能在后台线程上执行任何与UI相关的操作。 这里你试图在后台线程的textview中设置文本。 这就是为什么exception警报在主线程上运行它。 您可以更新主线程上的textview文本,如下所示。

尝试使用此代码设置文本:

 dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"viewcontroller response =%@",responseString); txt_output.text=[NSString stringWithFormat:@"%@",strResponse]; }); 

你正尝试从主线程以外的线程调用UIKit方法,这是不允许的。 exception实际上直接告诉你,在“原因”字段中:

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!' 

您应该在主线程上运行您的完成块,以便该块在主线程上执行的任何UIKit工作。