iOS app运行时报错: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 运行工程时报出下面的错误错信息,我还以为是什么呢,好久没遇到过这样的错误了。
**2016-11-17 14:47:20.205913 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  + 148**
** 1   libobjc.A.dylib                     0x0000000182df055c objc_exception_throw + 56**
** 2   CoreFoundation                      0x00000001843b6108  + 0**
** 3   Foundation                          0x0000000184f9dea4  + 192**
** 4   Foundation                          0x0000000184de53fc  + 36**
** 5   UIKit                               0x000000018ab5c770  + 72**
** 6   UIKit                               0x000000018a20e1e8  + 1140**
** 7   QuartzCore                          0x00000001876ce188  + 148**
** 8   QuartzCore                          0x00000001876c2e64  + 292**
** 9   QuartzCore                          0x00000001876c2d24  + 32**
** 10  QuartzCore                          0x000000018763f7ec  + 252**
** 11  QuartzCore                          0x0000000187666c58  + 512**
** 12  QuartzCore                          0x0000000187667124  + 660**
** 13  libsystem_pthread.dylib             0x000000018344afbc  + 572**
** 14  libsystem_pthread.dylib             0x000000018344ace4  + 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 app运行时报错: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.)