其他分享
首页 > 其他分享> > IOS 不在主线程操作UI

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.

 

在子线程中操作UI相关的操作了;

修改方式,将操作UI的代码块(注意代码块里不能有return)放到如下宏定义里

#ifndef dispatch_main_async_safe    
#define dispatch_main_async_safe(block)\
    if (dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL) == dispatch_queue_get_label(dispatch_get_main_queue())) {\
        block();\
    } else {\
        dispatch_async(dispatch_get_main_queue(), block);\
    }
#endif

 

//主线程同步队列  https://blog.csdn.net/qq_26341621/article/details/50156157
#define dispatch_main_sync_safe(block)\
    if ([NSThread isMainThread]) {\
        block();\
    } else {\
        dispatch_sync(dispatch_get_main_queue(), block);\
    }
//主线程异步队列
#define dispatch_main_async_safe(block)\
    if ([NSThread isMainThread]) {\
        block();\
    } else {\
        dispatch_async(dispatch_get_main_queue(), block);\
    }
//用法
  dispatch_main_async_safe(^{
                    //需要执行的代码片段;
                });

 

如果你放到代码块中的代码有return操作,就会报错:

No matching function for call to 'dispatch_async' in my drawing code:You start drawing on one thread, then finish it on another thread. That's a ticking time bomb.

标签:get,IOS,主线,dispatch,queue,UI,async,main,block
来源: https://www.cnblogs.com/8335IT/p/16285828.html