今天在写程序的时候,使用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. PCA(Principal Components Analysis)主成分分析

    全是图片..新手伤不起.word弄的,结果csdn传不了..以后改. .

  2. MongoDB之Java測试代码(DAO层)

    MongoInit.java是数据库初始化及连接类 MongoUtils.java是对mongodb的各种操作方法 MongoInit.java package com.wlwcloud.datate ...

  3. Codeforces 10A-Power Consumption Calculation(模拟)

    A. Power Consumption Calculation time limit per test 1 second memory limit per test 256 megabytes in ...

  4. Bmob移动后端云服务平台--Android从零開始--(二)android高速入门

    Bmob移动后端云服务平台--Android从零開始--(二)android高速入门 上一篇博文我们简介何为Bmob移动后端服务平台,以及其相关功能和优势. 本文将利用Bmob高速实现简单样例,进一步 ...

  5. Android语音播报、后台播报、语音识别

    Android语音播报.后台播报.语音识别 本文介绍使用讯飞语音实现语音播报.语音识别功能. 讯飞开放平台:http://www.xfyun.cn/index.php/default/index 程序 ...

  6. hdu-3401-Trade-单调队列优化的DP

    单调队列入门题... dp[i][j]:第i天.手中拥有j个股票时,获得的最大利润. 若第i天不买不卖:dp[i][j]=max(dp[i][j],dp[i-1][j]); 若第i天买         ...

  7. web forms page和control的生命周期life cycle交互,以及page生命周期中每个event中需要做什么事情

    只有 page_load和page_init这些可以autoeventwireup RenderControl只提供override public override void RenderContro ...

  8. js小知识 双叹号(!!)

    !!:一般用来将后面的表达式强制转换为布尔值(boolean):true或者false; avascript约定规则为:  false.undefinded.null.0.”” 为 false  tr ...

  9. Django(1.7 part1)

    django安装: django解压后目录下有一个setup.py文件,在命令行运行python setup.py install,当前前提是已经安装了python才能执行命令,然后用下面命令检查dj ...

  10. WPF 样式

    样式是属性值的集合,能被应用到一个元素,类似CSS,每个控件最多只能有一个样式,通过控件的Stype属性应用样式,如下代码,其中BigFontButtonStyle是用于检索资源的关键字,也可以通过代 ...