iOS开发,在main thread以外的thread更新UI
2017-02-22 17:27
423 查看
如果需要在异步任务(Async Task)中更新UI,若直接设置UI,会导致程序崩溃。
例如,在异步block中去更改UI:
运行时会崩溃,并报错,意思是此操作只能在主线程执行:
正确的方法是通过mainQueue向主线程队列添加block来执行更新UI,如下:
例如,在异步block中去更改UI:
NSOperationQueue *queue=[[NSOperationQueue alloc] init]; [queue addOperationWithBlock:^{ @autoreleasepool { // the other codes ... _textView.text = [_textView.text stringByAppendingString:stringResult]; } }];
运行时会崩溃,并报错,意思是此操作只能在主线程执行:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'
正确的方法是通过mainQueue向主线程队列添加block来执行更新UI,如下:
NSOperationQueue *queue=[[NSOperationQueue alloc] init]; [queue addOperationWithBlock:^{ @autoreleasepool { // the other codes ... [[NSOperationQueue mainQueue] addOperationWithBlock:^{ // update UI here _textView.text = [_textView.text stringByAppendingString:stringResult]; }]; } }];
相关文章推荐
- ios开发,将子线程获取的数据实时更新到UI
- iOS 开发中为什么更新UI都要放在主线程中?
- 『ANDROID』 在Android开发过程中,常Android 更新UI的两种方法——handler和runOnUiThread()
- android UI更新问题 Thread和Looper以及Handler和Message详解 Android开发必读
- ios开发 UI高级 更新Ruby
- [转载]iOS 开发中为什么更新UI都要放在主线程中?
- iOS开发,将子线程获取的数据实时更新到UI
- IOS开发,子线程中是不能更新UI的
- ios开发,将子线程获取的数据实时更新到UI
- ios开发,将子线程获取的数据实时更新到UI
- iOS开发-进阶:子线程更新UI的两种方法
- ios更新UI时请尝试使用performSelectorOnMainThread方法
- iOS开发 --- 推送 SDK: Main Thread Checker: UI API called on a background thread
- Android开发中更新UI时runOnUIthread(Runnable)与Handler.post(Runnable)的区别及如何选择
- ios更新UI时请尝试使用performSelectorOnMainThread方法
- iOS 开发中为什么更新UI都要放在主线程中?
- 【iOS开发-95】JSON反序列化、XML数据解析以及主线程中的UI更新等小细节
- iOS 开发中为什么更新UI都要放在主线程中?
- iOS开发之为什么更新UI都要放在主线程中
- 【解决】iOS开发,更新界面时崩溃,Tried to obtain the web lock from a thread other than the main thread or ……