今天在写程序的时候,使用Xcode 运行工程时报出下面的错误错信息,我还以为是什么呢,好久没遇到过这样的错误了。
**ProjectName[1512:778965] 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.**
** Stack:(**
** 0 CoreFoundation 0x00000001843b61d8 <redacted> + 148**
** 1 libobjc.A.dylib 0x0000000182df055c objc_exception_throw + 56**
** 2 CoreFoundation 0x00000001843b6108 <redacted> + 0**
** 3 Foundation 0x0000000184f9dea4 <redacted> + 192**
** 4 Foundation 0x0000000184de53fc <redacted> + 36**
** 5 UIKit 0x000000018ab5c770 <redacted> + 72**
** 6 UIKit 0x000000018a20e1e8 <redacted> + 1140**
** 7 QuartzCore 0x00000001876ce188 <redacted> + 148**
** 8 QuartzCore 0x00000001876c2e64 <redacted> + 292**
** 9 QuartzCore 0x00000001876c2d24 <redacted> + 32**
** 10 QuartzCore 0x000000018763f7ec <redacted> + 252**
** 11 QuartzCore 0x0000000187666c58 <redacted> + 512**
** 12 QuartzCore 0x0000000187667124 <redacted> + 660**
** 13 libsystem_pthread.dylib 0x000000018344afbc <redacted> + 572**
** 14 libsystem_pthread.dylib 0x000000018344ace4 <redacted> + 200**
** 15 libsystem_pthread.dylib 0x000000018344a378 pthread_mutex_lock + 0**
** 16 libsystem_pthread.dylib 0x0000000183449da4 start_wqthread + 4**
**)**

从上面的报错信息可以看出,主线程在运行的时候子线程修改了主线程UI的布局约束,在iOS开发中,所有的有关界面UI的更新操作必须在主线程中完成。这样的错误很容易出现在使用block的时候,因为我的block就是在子线程中进行的,所以回顾了刚才自己写的代码,发现还真是粗心了。
解决的办法就是把有关UI更新的代码操作放到主线程中就可以了。
修改前:

  [self.healthMgr stepCount:^(double steps, NSError *error) {
cell.stepsNumberLabel.text = [NSString stringWithFormat:@"%.0f 步数", steps];
}]; [self.healthMgr distance:^(double distance, NSError *error) {
cell.distanceLabel.text = [NSString stringWithFormat:@"%.02f 公里", distance];
}]; [self.healthMgr floorsClimbed:^(double floors, NSError *error) {
cell.floorsNumberLabel.text = [NSString stringWithFormat:@"%.0f 楼层", floors];
}];

修改后:

[self.healthMgr stepCount:^(double steps, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
cell.stepsNumberLabel.text = [NSString stringWithFormat:@"%.0f 步数", steps];
});
}]; [self.healthMgr distance:^(double distance, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
cell.distanceLabel.text = [NSString stringWithFormat:@"%.02f 公里", distance];
});
}]; [self.healthMgr floorsClimbed:^(double floors, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
cell.floorsNumberLabel.text = [NSString stringWithFormat:@"%.0f 楼层", floors];
});
}];

这时候,你可能会问block是在子线程执行的吗?
答:不一定。这个得看你执行block的时候,是哪种线程了,要是在主线程执行block,那么你的block里边的线程就是主线程了。否则就是子线程。

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.的更多相关文章

  1. android 不能在子线程中更新ui的讨论和分析

    问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要 ...

  2. Android多线程之(一)View.post()源码分析——在子线程中更新UI

    提起View.post(),相信不少童鞋一点都不陌生,它用得最多的有两个功能,使用简便而且实用: 1)在子线程中更新UI.从子线程中切换到主线程更新UI,不需要额外new一个Handler实例来实现. ...

  3. 如何在子线程中更新UI

    一:报错情况 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that creat ...

  4. Android在子线程中更新UI(二)

    MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...

  5. Android在子线程中更新UI(一)

    MainActivity如下: package cc.testui1; import android.os.Bundle; import android.os.Handler; import andr ...

  6. 使用Handler在子线程中更新UI

    Android规定仅仅能在主线程中更新UI.假设在子线程中更新UI 的话会提演示样例如以下错误:Only the original thread that created a view hierach ...

  7. Android开发UI之在子线程中更新UI

    转自第一行代码-Android Android是不允许在子线程中进行UI操作的.在子线程中去执行耗时操作,然后根据任务的执行结果来更新相应的UI控件,需要用到Android提供的异步消息处理机制. 代 ...

  8. C#子线程中更新ui

    本文实例总结了C#子线程更新UI控件的方法,对于桌面应用程序设计的UI界面控制来说非常有实用价值.分享给大家供大家参考之用.具体分析如下: 一般在winform C/S程序中经常会在子线程中更新控件的 ...

  9. Android 在子线程中更新UI

    今天在做练习时,在一个新开启的线程中调用“Toast.makeText(MainActivity.this, "登陆成功",Toast.LENGTH_SHORT).show();” ...

随机推荐

  1. HDU 1350 Taxi Cab Scheme

    Taxi Cab Scheme Time Limit: 10000ms Memory Limit: 32768KB This problem will be judged on HDU. Origin ...

  2. libvips

    libvips : an image processing library libvips is a 2D image processing library. Compared tosimilar l ...

  3. IE6浏览器不支持固定定位(position:fixed)解决方案(转)

    IE6浏览器不支持固定定位(position:fixed)解决方案   来源:互联网 作者:佚名 时间:12-04 10:54:05 [大 中 小] 点评:有些朋友在进行网页布局时,会遇到IE6浏览器 ...

  4. 12:打印 1 到最大的 n 位数

    题目:输入数字 n.按顺序打印出从 1 到 最大的 n 位十进制数.比方输入 3 ,则打印出 1.2 .3 一直到最大的3位数即 999. 解析: easy知道不能用 int 等数字类型表示(大数问题 ...

  5. 【Allwinner ClassA20类库分析】 2.free pascal语法及结构简析

        上一节介绍了Lazarus一般的开发操作流程,对于不熟悉pascal语言的朋友可能看的还是不大明确.不知道pascal代码里都应该包括什么或起什么作用.这回就简单地介绍下语法及代码文件的结构. ...

  6. call to OpenGL ES API with no current context 和Fatal signal 11

    近日在用cocos2dx3.4的时候使用了JNI调用,发现一个现象 当不使用jni的时候全然正常.使用了jni后回去的全部文字都变成黑块,而且有概率程序崩溃.附带出了两个log call to Ope ...

  7. linux定时备份mysql数据库文件

    1.设定定时器:终端敲入:crontab -e命令 2,然后写入     00 23 * * * /home/db_bak_file/dbbak.sh >>/home/db_bak_fil ...

  8. zzulioj--1790-- 弹珠游戏(数学水题!)

    弹珠游戏 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 14  Solved: 10 SubmitStatusWeb Board Descriptio ...

  9. java布局管理

    FlowLayout :组件在一行中按加入的先后顺序从左至右水平排列,排满后折行,每行中的组件都居中排列.BorderLayout:把容器空间划分为北.南.西.东.中五个区,每加入一个组件都应说明把这 ...

  10. 你不知道的JavaScript(三)字符串

    JavaScript字符串很容易被认为本质就是字符数组,ECMAScript规范中字符串作为一种单独的string类型,它的底层实现可能是数组,也可能是其他数据结构,因不同的JavaScript引擎而 ...