Source — https://www.wallpaperflare.com/several-colors-silk-thread-in-needle-wallpaper-17935

Main Thread Checker in XCode

Niki Trivedi

--

I’ve been using Main thread checker from XCode 9, And It feels amazing if some tool is there to point out your mistakes (instead of a human being😅).

What It does ?

Basically It will verify your code and will point out code which should be in main thread instead of background. It can evaluate code which is written in Swift and C language.

How to enable ?

  1. Edit scheme.
  2. Select run scheme.
  3. Select the Diagnostics tab.
  4. Check the Main Thread Checker Check Box.
  5. Click on small arrow written besides MTC text (Optional).

On click on arrow message besides main thread checker will add breakpoint for the project for debugging.

Screenshots for reference

How it works ?

During runtime it will check if all UI and other code which should be run in main thread is working or not, incase it is not working it will pause at the line of code which should be put in main thread along with appropriate warning message.

I tried to create AppDelegate instance object in background thread at that time It is giving me error message.

If main thread debugger break point is not added and one wish to identify issues without that then you can get a look at the list of all the main thread issues in runtime error messages.

When i tried to create AppDelegate instance method in background for testing purpose same as above screenshot.

Why to use ?

Threading is a very powerful thing, if one knows the beauty of it and can use it in correct way just like boolean. Threading can lead to some major problems. So it’s better to take precautions by using such facility provided by apple. To get in depth of this developers can use profiler too.

There are lot of other features which developer can leverage for sanity checking of code(can view in the below screenshots), like Thread sanitizer and other breakpoints provided by XCode..

--

--

Niki Trivedi

I'm software engineer who's trying to give back to the community by writing mini blogs on the regular topics that I learn day to day.