在主线程中做UI操作

就像很多大道理我们都知道,但是现实生活中往往会做出不同的行为。
开发中,网络请求、UI操作等都需要在主线程中做操作,说起来都知道,今天,遇到一个问题:

报错

2017-03-30 17:22:24.486423+0800 qas[1340:315782] 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                      0x0000000191772ff0  + 148
     1   libobjc.A.dylib                     0x00000001901d4538 objc_exception_throw + 56
     2   CoreFoundation                      0x0000000191772f20  + 0
     3   Foundation                          0x0000000192364338  + 128
     4   Foundation                          0x00000001923640b8  + 76
     5   Foundation                          0x00000001921b0af8  + 132
     6   Foundation                          0x0000000192362d40  + 112
     7   UIKit                               0x000000019789f344  + 1692
     8   QuartzCore                          0x0000000194a8f274  + 148
     9   QuartzCore                          0x0000000194a83de8  + 292
     10  QuartzCore                          0x0000000194a83ca8  + 32
     11  QuartzCore                          0x00000001949ff360  + 252
     12  QuartzCore                          0x0000000194a263c0  + 504
     13  QuartzCore                          0x0000000194a266ec  + 244
     14  libsystem_pthread.dylib             0x0000000190833ef0  + 572
     15  libsystem_pthread.dylib             0x0000000190833c18  + 200
     16  libsystem_pthread.dylib             0x00000001908332a8 _pthread_wqthread + 1312
     17  libsystem_pthread.dylib             0x0000000190832d7c start_wqthread + 4
     )

其实看到后 就大概猜到有个UI操作未在主线程。

简单模拟大概是这样子的:

异步请求数据,成功或失败后,结束刷新(刷新使用的是MJRefresh)

结束刷新的操作应该放在主线程

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //这里做网络请求
        //成功或失败的时候 结束刷新
         dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.scrollview.mj_header endRefreshing];
         });
    });

希望自己能更多经验、对知识有更深刻的理解、实践中加深记忆与理解

你可能感兴趣的:(在主线程中做UI操作)