我正在尝试在textview中显示输出字符串,但它会将其删除。 请检查
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,但在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)
答案 0 :(得分:2)
您尝试从主线程以外的线程调用UIKit方法,这是不允许的。这个例外实际上直接告诉你这个&#34;原因&#34;字段:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'
你应该在主线程上运行你的完成块,这样任何UIKit工作块都会在主线程上发生。
答案 1 :(得分:1)
您无法在后台线程上执行任何与UI相关的操作。在这里,您尝试在后台线程的textview中设置文本。这就是为什么在主线程上运行异常警报的原因。您可以在主线程上更新textview文本,如下所示。
尝试使用以下代码设置文字:
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"viewcontroller response =%@",responseString);
txt_output.text=[NSString stringWithFormat:@"%@",strResponse];
});