今天在写程序的时候,使用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. HTML5简单进度环插件

    前几天做了一个进度条的插件.今天我用HTML5的arc做一个简单的进度环的插件. 代码演示 事实上非常easy的.相同,我们先用一个实例: 配置js代码 var setting = { id: &qu ...

  2. Hibernate的多种关系映射(oto、otm、mtm)

    前提:使用注解映射 一.一对一(夫妻关系表) 两个表:hus1和wife1表,外键为id,各自有名字hname和wname 映射得到两个类:Hus1和Wife1类 Hus1类(主表): package ...

  3. 从头认识java-15.3 使用HashSet须要注意的地方

    这一章节我们来讨论一下使用Set的各种实现须要注意的地方. Set接口的经常使用实现类有:HashSet.TreeSet,LinkedHashSet 1.HashSet 大家对于HashSet的印象都 ...

  4. 用自定义的函数将gps转换为高德坐标

    <?php echo<<<_END <!doctype html> <html> <head> <meta charset=" ...

  5. JAVA设计模式之【模板方法模式】

    模板方法模式 提高代码的复用性 把常用的基本方法放入父类中 强调一种流程步骤 角色 抽象类 抽象方法 具体方法 钩子方法 空方法 通过bool控制 具体类 看例子 1.银行模板类 package Te ...

  6. xBIM 基础16 IFC的空间层次结构

    系列目录    [已更新最新开发文章,点击查看详细]  本篇介绍如何从文件中检索空间结构.IFC中的空间结构表示层次结构的嵌套结构,表示项目,站点,建筑物,楼层和空间.如果您查看IFC文档, 您会发现 ...

  7. 页面的URL分析----window.location.href

    window.location是页面的位置对象window.location.href是 location的一个属性值,并且它是location的默认属性. window.location直接赋值一个 ...

  8. 《Unix环境高级编程》读书笔记 第7章-进程环境

    1. main函数 int main( int argc, char *argv[] ); argc是命令行参数的数目,包括程序名在内 argv是指向参数的各个指针所构成的数组,即指针数组 当内核执行 ...

  9. 3ds Max制作客厅场景实例教程

    附件系列 (图01) 让我们回顾一下场景:一个房间包括下列一件件家具, 在中间的一张小桌子,在房间的角落的一个小桌子,有一个垃圾桶和一个带镜子的边桌,有一个烛台.还有一个挂钟,窗帘,沙发和带手臂的椅子 ...

  10. [ZJOI2006]物流运输 最短路 动态规划

    Code: 定义状态 $dp[i]$ 为前 $i$ 天的最小代价. 状态转移为:$dp[i]=min(dp[i],dp[j]+spfa(j+1,i)$ 这里 $spfa(i,j)$ 是指 $(i,j) ...