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.

今天在写程序的时候,使用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里边的线程就是主线程了。否则就是子线程。

 

posted @ 2018-08-06 11:39  Mr.pengge  阅读(614)  评论(0编辑  收藏  举报