简体   繁体   中英

What is Main Thread Checker in Xcode

I checked what's new in Xcode 9 documentation and I found this

在此处输入图像描述

But i didn't understand what is that how I can use this with new 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 例子

From Apple documentation :

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.

So for example trying to change the text property of a UILabel on a background thread will not work. Apple says that this can result in missed UI updates, visual defects, data corruptions, and crashes . In practice, 99% of the time this will result in random missed UI updates and visual defects (and not crashes).

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. 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 . Once you've detected a use of UIKit on a background thread, you can solve it using DispatchQueue .

Again, from Apple documentation :

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.

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.

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. 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.

As @hamish points out, you can also watch the WWDC video for a more detailed explanation.

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

在此处输入图像描述

在此处输入图像描述

In XCODE-12 Go to debug then select Scheme then edit scheme Select Run -> Diagnostics

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM