今天在写程序的时候,使用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. JAVA深克隆与浅克隆1

    复制就是得到一个副本 克隆就是复制一个对象的复本.但一个对象中可能有基本数据类型,如:int,long,float    等,也同时含有非基本数据类型如(数组,集合等)被克隆得到的对象基本类型的值修改 ...

  2. SQLSERVER-存储过程知识点

    原文链接:http://www.qeefee.com/article/000566 存储过程是一组预编译的SQL语句,它可以包含数据操纵语句.变量.逻辑控制语句等. 存储过程允许带参数: 输入参数:可 ...

  3. UE4在VS2013中各个编译配置代表意义

    UE4中有个各式各样的编译配置,都怎么个意思呢? 对原文的理解和翻译. https://docs.unrealengine.com/latest/INT/Programming/Development ...

  4. Python菜鸟晋级12----多线程

    Python 多线程 多线程类似于同一时候执行多个不同程序,多线程执行有例如以下长处: 使用线程能够把占领长时间的程序中的任务放到后台去处理. 用户界面能够更加吸引人.这样比方用户点击了一个butto ...

  5. Default Document <defaultDocument> IIS中的默认页面

    https://docs.microsoft.com/en-us/iis/configuration/system.webserver/defaultdocument/index Default do ...

  6. ThinkPHP5.0框架开发--第10章 TP5.0验证器

    ThinkPHP5.0框架开发--第10章 TP5.0验证器 第10章 TP5.0验证器 ======================================= 今日学习 1.验证器 1) 控 ...

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

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

  8. SPSS学习小记

    2013年1月8日 最近一直在SPSS中处理数据,涉及到函数部分,不是太懂,特记录于此,以便翻阅.   SPSS判断字符变量中是否含有某字符串的表示方式:  (INDEX(url,'ad')>0 ...

  9. ASP调用WebService转化成JSON数据,附json.min.asp

    首先定义SOAP数据,然后创建HTTP对象,然后使用POST提交,获取状态码为200,就说明调用成功,再进行下一步操作…… <!--#Include virtual="/Include ...

  10. grant 命令

    创建拥有所有权限账户.可以远程连接.并且允许用户再将该权限授予其它用户: grant all privileges on *.* to root @"%" identified b ...