IOS 不在主執行緒操作UI
阿新 • • 發佈:2022-05-18
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操作,就會報錯: