今天在写程序的时候,使用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. Ruby中写换行

    Ruby中写换行 print("Hello,\nRuby\n!\n") print("Hello, Ruby ! ") 这两个竟然是一样的:就是说,可以直接回车 ...

  2. Oracle性能分析1:开启SQL跟踪和获取trace文件

    当Oracle查询出现效率问题时,我们往往须要了解问题所在,这样才干针对问题给出解决方式.Oracle提供了SQL运行的trace信息,当中包括了SQL语句的文本信息.一些运行统计,处理过程中的等待, ...

  3. 怎么用命令行运行jar文件

    假设你配置好了jre环境,你如今有一个打包好的jar文件,你能够这样子開始运行 java -classpath example.jar mainClass -classpath告诉虚拟机在哪里找类的字 ...

  4. 【MongoDB】深入了解MongoDB不可不知的十点

    一.对象ID的生成 每一个mongoDB文档那个都要求有一个主键.它在每一个集合中对全部的文档必须是唯一的.主键存放在文档_id字段中.由12个字符组成: 4c291856       238d3b  ...

  5. dom 编程(html和xml)

    html dom与xml dom关系: 什么是 DOM? DOM 是 W3C(万维网联盟)的标准. DOM 定义了訪问 HTML 和 XML 文档的标准: "W3C 文档对象模型 (DOM) ...

  6. 剪切具有CornerRadius的RectangleGeometry(可能在Ripple中用到)

    剪切具有CornerRadius的RectangleGeometry(可能在Ripple中用到) 1.新建Converter public class BorderClipConverter : IM ...

  7. vue keep-alive保存路由状态2 (高级用法,接上篇)

    接上篇 https://www.cnblogs.com/wangmaoling/p/9803960.html 本文很长,请耐心看完分析. 4.高级用法,指定从什么组件进入才缓存,以及销毁缓存:先介绍我 ...

  8. 移动端fixed后 横竖屏切换时上部或下部出现空隙问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. hbase的优化(全)

    高可用 在HBase中Hmaster负责监控RegionServer的生命周期,均衡RegionServer的负载,如果Hmaster挂掉了,那么整个HBase集群将陷入不健康的状态,并且此时的工作状 ...

  10. APP信息获取接口

    https://itunes.apple.com/lookup?id=APPID&callback=2 http://myapp.com/cgi-bin/mapp/mapp_info?type ...