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()

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

  1. 转载自~浮云比翼:Step by Step:Linux C多线程编程入门(基本API及多线程的同步与互斥)

    Step by Step:Linux C多线程编程入门(基本API及多线程的同步与互斥)   介绍:什么是线程,线程的优点是什么 线程在Unix系统下,通常被称为轻量级的进程,线程虽然不是进程,但却可 ...

  2. [转]Delphi多线程编程入门(二)——通过调用API实现多线程

    以下是一篇很值得看的关于Delphi多线程编程的文章,内容很全面,建议收藏. 一.入门 ㈠. function CreateThread(    lpThreadAttributes: Pointer ...

  3. HOOK API 在多线程时应该注意的问题点

    在使用INLINE HOOK API实现对系统API的拦截时,正常情况下并没有太大问题,但一旦涉及到多线程,不管是修改IAT还是JMP,2种方法均会出现不可预料的问题,特别是在HOOK一些复杂的大型系 ...

  4. Step by Step:Linux C多线程编程入门(基本API及多线程的同步与互斥)

    介绍:什么是线程,线程的优点是什么 线程在Unix系统下,通常被称为轻量级的进程,线程虽然不是进程,但却可以看作是Unix进程的表亲,同一进程中的多条线程将共享该进程中的全部系统资源,如虚拟地址空间, ...

  5. [C++] Win32 API 的多线程Timer管理Trick - 利用PostThreadMessage

    有时候我们需要在程序里定时地完成一些任务, 比如5秒后发送, 10秒后弹窗之类的操作. 这就需要一个类似于定时器的组件. 这个组件在windows.h里被称为Timer. 设置一个Timer 第一步当 ...

  6. VC中多线程(一)Win32 API对多线程编程的支持

    http://blog.sina.com.cn/s/blog_4ae08ad801008yer.html

  7. Dynamics CRM2016 使用web api来创建注释时的注意事项

    在使用wei api 创建注释的时候,有个字段需要注意下,就是下面图中的objectid字段,虽然它是个查找字段,但不能像普通的查找字段property@odata.bind来赋值 上代码,注意看倒数 ...

  8. MySQL8安装及使用当中的一些注意事项

    前言 这两天构建新项目,在本地安装的mysql8(本地环境windows),期间忘了密码,又卸载重装了一番,然后捣鼓了一顿授权给别人访问,最后磕磕绊绊的搞好了,下面是在这过程中遇到的问题及解决办法小结 ...

  9. Winform实现用多线程、百度地图API解析某公司的物理地址

    前言 作为一个很挫的C#新手总喜欢自己写点儿不着边际的东西,本人是个新手加菜鸟,写B/S的,工作中,任务完成了,空闲下来,总想继续学点儿什么,由此触发了本篇文章了.个人一直认为,.NET中,C/S所要 ...

随机推荐

  1. Vscode 开发插件

    vs常用公共插件 Auto Close Tag 自动闭合标签 Auto Rename Tag 自动重命名标签 AutoFileName 自动联想文件名 Autoprefixer 自动兼容前缀 Auto ...

  2. VSCode提示没有权限,无法保存文件问题

    重装了系统之后,重新打开了VSCode发现无法保存修改的文件,激活系统后发现还是无法保存文件,都是提示权限问题,原因在于文件夹权限继承并不是我所登录的这个用户,接着我试着按照网上的方法,在文件夹后,右 ...

  3. docker(整理中

    docker镜像默认的下载地址就是docker的官网,而他们的官网在国内没有节点,时不时就被国家防火墙隔绝,会出现DNS解析不到,或者找不到镜像等狗血提示. 解决的方法有三个: 第一,就是不断尝试,因 ...

  4. 小白学 Python 爬虫(30):代理基础

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  5. Web_telnet 通过JAVA实现网页上面的命令行远程工具 以及对于流的理解

    前言 这几天研究了一下如何在web页面上通过telnet 连接一个远程的机器,通过命令行进行控制,让然,B/S架构的项目,如果直接通过 浏览器是无法和远程机器进行通信的,我们就得借助后端来帮助实现这样 ...

  6. 利用Redis实现集群或开发环境下SnowFlake自动配置机器号

    前言: SnowFlake 雪花ID 算法是推特公司推出的著名分布式ID生成算法.利用预先分配好的机器ID,工作区ID,机器时间可以生成全局唯一的随时间趋势递增的Long类型ID.长度在17-19位. ...

  7. C / C++ 保留小数函数(setprecision(n)的一些用法总结)

    从C语言开始正式学习C++,但是一上来输出位数就懵了,查资料才知道C++需要使用 “ setprecision  ”函数.自己总结一下. 首先说C++代码 #include <iomanip&g ...

  8. Windows To Go 企业版2019 LTSC 开发环境部署

    Windows To Go 是一项非常实用的功能,与传统方式安装Windows 10相比更具有灵活性,会根据每次接入的硬件型号保留不同版本驱动. 由于博主是一名全栈程序员(截至发稿处于菜鸟级别),对灵 ...

  9. 异常记录——bat批处理闪退

    bat批处理闪退 bat描述 我的博客每次更新需要跑多个命令 clean(清除旧文)+g(生成新文)+d(部署到服务器),作为一个懒惰的程序员,自然要写一个bat一键完成 E: cd blog hex ...

  10. linux下卸载旧版本cmake安装新版本cmake

    1.看当前cmake版本 cmake --version 2.卸载旧版本下的cmake apt-get autoremove cmake 3.安装新版面cmake http://www.cnblogs ...