简体   繁体   English

什么是 Xcode 中的主线程检查器

[英]What is Main Thread Checker in Xcode

I checked what's new in Xcode 9 documentation and I found this我检查了 Xcode 9 文档中的新内容,发现了这个

在此处输入图像描述

But i didn't understand what is that how I can use this with new Xcode 9.但我不明白我如何在新的 Xcode 9 中使用它。

It can be enabled/disabled in the diagnostics option of the scheme.它可以在方案的诊断选项中启用/禁用。 Besides, the "Pause on issues" is a comfortable option to debug these problems.此外,“问题暂停”是调试这些问题的舒适选项。

Xcode 11 Xcode 11 在此处输入图像描述

Xcode <11 Xcode <11 例子

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.因此,例如尝试在后台线程上更改UILabeltext属性将不起作用。 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 视频以获得更详细的解释。

Under the Runtime API Checking section, ensure that the Main Thread Checker is enabled to see if you are executing ui methods in a non-UI thread在 Runtime API Checking 部分下,确保启用Main Thread Checker以查看您是否在非 UI 线程中执行 ui 方法

在此处输入图像描述

在此处输入图像描述

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.

相关问题 ARKit模板Xcode项目Main Thread Checker日志控制台 - ARKit template Xcode project Main Thread Checker log console Xcode 10 主线程检查器:Cordova 相机插件 - Xcode 10 Main Thread Checker: Cordova Camera Plugin iOS / Xcode:主线程检查器似乎没有激活,但由于在后台线程上调用UI而导致崩溃? - iOS/Xcode: Main Thread Checker doesn't seem to be activating, but still a crash due to UI being called on background thread? Swift 本地通知检查器仅主线程 - Swift Local Notifications Checker Main Thread Only AdMob 广告 - 日志中的主线程检查器错误 - AdMob Ads - Main Thread Checker error in log 主线程检查器:在后台线程上调用的 UI API - Main Thread Checker: UI API called on a background thread 主线程检查器:在后台线程上调用的UI API:-[UIApplication委托] - Main Thread Checker: UI API called on a background thread: -[UIApplication delegate] CoreMotion主线程检查器警告,仅出现在2018型号iPhone上 - Main Thread Checker warning with CoreMotion, only appearing on 2018 model iPhones Xcode线程1:它总是主UI线程吗? - Xcode Thread 1 : Is it always main UI thread? 我可以关闭主线程检查器,并在关闭主线程时使用 UIKit 吗? - Can I turn off Main Thread Checker, and use UIKit on off main thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM