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)放到如下宏定义里

1
2
3
4
5
6
7
8
#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.

posted on   邗影  阅读(420)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-05-18 Linux 声音采集的时候内容全都是0
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示