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. Vue的数据双向绑定和Object.defineProperty()

    Vue是前端三大框架之一,也被很多人指责抄袭,说他的两个核心功能,一个数据双向绑定,一个组件化分别抄袭angular的数据双向绑定和react的组件化思想,咱们今天就不谈这种大是大非,当然我也没到达那 ...

  2. SPOJ - PHRASES Relevant Phrases of Annihilation (后缀数组)

    You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages ...

  3. selenium经过WebDriverWait实现ajax测试

    当前位置:我的异常网» Web前端 » selenium经过WebDriverWait实现ajax测试 selenium经过WebDriverWait实现ajax测试 www.MyException. ...

  4. IdentityServer4 Resources

    原文地址 Resources 的定义 通常在系统中是顶一个需要保护的资源.这些资源可是用户的信息,比如身份信息或者邮箱地址,也可以是某些API的访问权限. Note: 可以通过C#的对象模型或者通过数 ...

  5. 第二阶段:4.商业需求文档MRD:2.PRD-功能详细说明

    功能详细说明 功能一般分两类:1.通用性功能(导航,菜单项) 通过PRD让各个部门清楚的了解产品的各项功能构成.

  6. shellcode超级反杀

    shellcode超级免杀 作者声明: 本文章属于作者原创,不能转载,违反网络安全法自己承担.这里只供学习使用. 日期: 2019-12-30 我试过了电脑管家,火绒安全,360....一系列杀毒软件 ...

  7. 高校表白app用后感

    此款app前提是我们自行设计研发的app,其次满足了在校大学生对谈恋爱的需求,可以说是一款简单实用,好操作的软件.下面我根据自己的使用体验简单的介绍一下此软件: (1)首先对于未使用过的用户,你需要在 ...

  8. .NET Core 3.1之深入源码理解HealthCheck(二)

    写在前面 前文讨论了HealthCheck的理论部分,本文将讨论有关HealthCheck的应用内容. 可以监视内存.磁盘和其他物理服务器资源的使用情况来了解是否处于正常状态. 运行状况检查可以测试应 ...

  9. C Primer Plus(二)

    重读C Primer Plus ,查漏补缺 重读C Primer Plus,记录遗漏的.未掌握的.不清楚的知识点 分支和跳转 1.ctype.h头文件里包含了一些列用于字符判断的函数,包括判断数字.大 ...

  10. 小小知识点(二十六)关于5G发展的28个核心问题,来自华为内部的深度解读

    本文来自微信公众号“腾讯深网”(ID:qqshenwang),作者 马关夏.36氪经授权转载. 一.5G先进性与行业应用 1.5G到底是什么?和4G比有什么不一样? 从国际电信联盟(ITU)的定义来看 ...