iOS开发,在main thread以外的thread更新UI
如果需要在异步任务(Async Task)中更新UI,若直接设置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开发,在main thread以外的thread更新UI的更多相关文章
- iOS开发-多线程编程技术(Thread、Cocoa operations、GCD)
简介 在软件开发中,多线程编程技术被广泛应用,相信多线程任务对我们来说已经不再陌生了.有了多线程技术,我们可以同做多个事情,而不是一个一个任务地进行.比如:前端和后台作交互.大任务(需要耗费一定的时间 ...
- 提高iOS开发效率的第三方框架等--不断更新中。。。
1. Mantle Mantle 让我们能简化 Cocoa 和 Cocoa Touch 应用的 model 层.简单点说,程序中经常要进行网络请求,请求到得一般是 json 字符串,我们一般会建一个 ...
- iOS开发常用小技巧记录(持续更新)
以下问题都是自己在项目中遇到的,解决问题的方法肯定有多种,我所列举的不一定就是最好的解决办法.如有问题欢迎大家指正,补充,交流. 解决同时按两个按钮进两个view的问题.[button setExcl ...
- iOS开发中检测版本,有新版本则更新
iOS程序自动提示更新的实现方案大致分为两种: 第一种,自己服务器提供一个接口,告知相关app的当前版本,是否需要更新,以及更新的地址等信息 . 第二种,就是利用苹果的appstore 提供的相关ap ...
- IOS开发--待研究源码(持续添加更新)
1.丰富的CAEmitterLayer制作的粒子效果,比如烟花效果 (还未研究) 该项目本人未研究,待以后有时间或者有需求再研究 github源码下载地址:https://github.com/lic ...
- 安卓开发之利用runOnUiThread在子线程更新UI
package com.lidaochen.test; import android.graphics.Bitmap; import android.graphics.BitmapFactory; i ...
- iOS开发-多线程开发之线程安全篇
前言:一块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源,比如多个线程访问同一个对象.同一个变量.同一个文件和同一个方法等.因此当多个线程访问同一块资源时,很容易会发生数据错误及数据不安 ...
- iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍
UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...
- iOS 报错:(子线程中更新UI)This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
今天在写程序的时候,使用Xcode 运行工程时报出下面的错误错信息,我还以为是什么呢,好久没遇到过这样的错误了. **ProjectName[1512:778965] This application ...
随机推荐
- 【leetcode刷题笔记】Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【leetcode刷题笔记】Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 常用CSS设置
主要内容: 一.容器类 二.文本类 三.特效类 一.容器类 1.background-image:url('img/02.gif'); 设置背景图(可以是动态图) 2.background-col ...
- Centos安装ntfs
ntfs优盘插在Linux上是无法直接使用的,需要安装ntfs插件才可使用 centos上安装ntfs-3g 下载ntfs-3g安装包,上传至需要安装的服务器并解压 cd 进入ntfs-3g目录,依次 ...
- 转战github了
现在已经改在github写博客了,地址为http://connorzhangxu.github.io/ 博客园用了几年,总体感觉不错,但是对公式的支持整体不是很好,所以后来自己搭建了github博客, ...
- codevs1281 Xn数列
题目描述 Description 给你6个数,m, a, c, x0, n, g Xn+1 = ( aXn + c ) mod m,求Xn m, a, c, x0, n, g<=10^18 输入 ...
- 图解Skip List——本质是空间换时间的数据结构,在lucene的倒排列表,bigtable,hbase,cassandra的memtable,redis中sorted set中均用到
Skip List的提出已有二十多年[Pugh, W. (1990)],却依旧应用广泛(Redis.LevelDB等).作为平衡树(AVL.红黑树.伸展树.树堆)的替代方案,虽然它性能不如平衡树稳定, ...
- L103
Give everyday the chance to become the most beautiful day of your life.把每天都过成你生命中最美好的一天.competence 能 ...
- hdu-5650 so easy(水题)
题目链接: so easy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- mysqllog
-- mysql delete log online 1 mysql命令purge mysql> purge master logs to "mysql-bin.000410&quo ...