UIKit, AppKit, 以及其他API在多线程当中的使用注意事项

Overview

The Main Thread Checker is a standalone tool for Swift and C languages that detects invalid usage of AppKit, UIKit, and other APIs on a background thread. Updating UI on a thread other than the main thread is a common mistake that can result in missed UI updates, visual defects, data corruptions, and crashes.

How the Main Thread Checker Works

At app launch, the Main Thread Checker dynamically replaces the implementations of methods that should only be called on the main thread with a version that prepends the check. Methods known to be safe for use on background threads are excluded from this check.

Note

Unlike other code diagnostic tools, the Main Thread Checker doesn't require recompilation, and can be used with existing binaries. You can run it on a macOS app without the Xcode debugger, such as on a continuous integration system, by injecting the dynamic library file located at /Applications/Xcode.app/Contents/Developer/usr/lib/libMainThreadChecker.dylib.

Performance Impact

The performance impact of the Main Thread Checker is minimal, with a 1–2% CPU overhead and additional process launch time of <0.1 seconds.

Because of its minimal performance overhead, the Main Thread Checker is automatically enabled when you run your app with the Xcode debugger.

Updating UI from a Completion Handler

Long-running tasks such as networking are often executed in the background, and provide a completion handler to signal completion. Attempting to read or update the UI from a completion handler may cause problems.

let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if let data = data {
self.label.text = "\(data.count) bytes downloaded"
// Error: label updated on background thread
}
}
task.resume()

Solution

Dispatch the call to update the label text to the main thread.

let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if let data = data {
DispatchQueue.main.async { // Correct
self.label.text = "\(data.count) bytes downloaded"
}
}
}
task.resume()

最新文章

  1. maven install时报错Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile
  2. MMS源码中异步处理简析
  3. 欧几里德与扩展欧几里德算法 Extended Euclidean algorithm
  4. WPF笔记
  5. Python学习笔记-字符串
  6. SqlSever基础 convert 将类型为字符的一列转成Int类型后进行排序
  7. 会&quot;说话&quot;的勒索病毒Cerber
  8. Linux---More命令 初级实现
  9. HTML与CSS入门——第十一章  在网页中使用图像
  10. 工具:BT Sync 同步文件
  11. 关于xhEditor
  12. 为什么getline()后要两次回车????(将输入的字符串按单词倒序输出)
  13. Spring4 IOC详解
  14. NFC驱动调试
  15. [转]在Windows下编译ffmpeg完全手册
  16. 痞子衡嵌入式:ARM Cortex-M内核那些事(4)- 性能指标
  17. CentOS 6下 Oracle11gR2 设置开机自启动
  18. NHibernate之旅系列文章导航
  19. iOS.CodeSign
  20. centos7搭建docker私有仓库

热门文章

  1. HDU - 5015 233 Matrix (矩阵快速幂)
  2. 【Linux】nl笔记
  3. There is already an open DataReader associated with this Connection which must be closed first EF
  4. read、write 与recv、send区别 gethostname
  5. ARM裸机开发之交叉工具链和MakeFile工程管理
  6. sed &amp; awk &amp; grep 专题
  7. 阿里巴巴java开发手册学习记录,php版
  8. 网络状态诊断工具——netstat命令
  9. shell脚本配置maven
  10. 洛谷P1776 宝物筛选 题解 多重背包