[英]What is Main Thread Checker in Xcode
From Apple documentation :来自Apple 文档:
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.
Main Thread Checker 是一款用于 Swift 和 C 语言的独立工具,可检测 AppKit、UIKit 和后台线程上其他 API 的无效使用。 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.
在主线程以外的线程上更新 UI 是一个常见的错误,可能会导致错过 UI 更新、视觉缺陷、数据损坏和崩溃。
So for example trying to change the text
property of a UILabel
on a background thread will not work.因此,例如尝试在后台线程上更改
UILabel
的text
属性将不起作用。 Apple says that this can result in missed UI updates, visual defects, data corruptions, and crashes . Apple 表示,这可能会导致错过 UI 更新、视觉缺陷、数据损坏和崩溃。 In practice, 99% of the time this will result in random missed UI updates and visual defects (and not crashes).
实际上,99% 的时间这将导致随机错过 UI 更新和视觉缺陷(而不是崩溃)。
Crashes would actually be good because we could detect easily such improper use of UIKit
, but random visual defects are much harder to detect during development.崩溃实际上是件好事,因为我们可以很容易地检测到
UIKit
的这种不当使用,但随机视觉缺陷在开发过程中很难检测到。 And that's where the Main Thread Checker comes in.这就是主线程检查器的用武之地。
The Main Thread Checker will help dectect uses of UIKit
on a background thread, it will not solve them .主线程检查器将帮助检测
UIKit
在后台线程上的使用,它不会解决它们。 Once you've detected a use of UIKit
on a background thread, you can solve it using DispatchQueue
.一旦检测到在后台线程上使用了
UIKit
,就可以使用DispatchQueue
解决它。
Again, from Apple documentation :同样,来自Apple 文档:
The documentation of URLSession
says that the completion closure will be called on a background thread, so this is bad, the Main Thread Checker will help you detect the use of UIKit on a background thread. URLSession
的文档说完成闭包将在后台线程上调用,所以这很糟糕,Main Thread Checker 将帮助您检测后台线程上 UIKit 的使用。
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: Use DispatchQueue.main
to perform UI updates on the main thread.解决方案:使用
DispatchQueue.main
在主线程上执行 UI 更新。
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()
The solution itself has nothing to do with Xcode, it's a feature of the language.解决方案本身与 Xcode 无关,它是该语言的一个特性。 So obviously it was possible in previous versions of Xcode, but before Xcode 9 you didn't have the Main Thread Checker to help you detect the problem.
所以很明显,在以前的 Xcode 版本中这是可能的,但是在 Xcode 9 之前,您没有 Main Thread Checker 来帮助您检测问题。
As @hamish points out, you can also watch the WWDC video for a more detailed explanation.正如@hamish 指出的那样,您还可以观看WWDC 视频以获得更详细的解释。
In XCODE-12 Go to debug then select Scheme then edit scheme Select Run -> Diagnostics在 XCODE-12 中转到调试然后选择方案然后编辑方案选择运行-> 诊断
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.