What Is Main Thread Checker in Xcode

What is Main Thread Checker in Xcode

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
Sample Image

Xcode <11
Example

Xcode 10 Main Thread Checker: Cordova Camera Plugin

It would seem that UIImagePickerController needs to be called in the UI Thread.
Fortunately, the code is already ready for that change!

Just move the cameraPicker init section into the main thread section:

dispatch_async(dispatch_get_main_queue(), ^{
// UI MainThread execution
CDVCameraPicker* cameraPicker = [CDVCameraPicker createFromPictureOptions:pictureOptions];
weakSelf.pickerController = cameraPicker;

cameraPicker.delegate = weakSelf;
cameraPicker.callbackId = command.callbackId;
// we need to capture this state for memory warnings that dealloc this object
cameraPicker.webView = weakSelf.webView;
...
}

D.

Main Thread Checker: UI API called on a background thread: -[UIApplication delegate]

If these reporting messages confuse you uncheck them:

  1. Edit Scheme...Sample Image
  2. Uncheck "Main Thread Checker" in Run > Diagnostics Sample Image

Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]

First, make sure your invocations of google maps and ui changes are called from the main thread.

You can enable Thread Sanitizer option in Xcode using below steps:

Screenshot

You can put offending lines on the main thread with the following:

DispatchQueue.main.async {
//Do UI Code here.
//Call Google maps methods.
}

Also, update your current version of google maps. Google maps had to make a couple of updates for the thread checker.

For the question: "Why would this be occurring?" I think Apple added an assertion for an edge case which Google then had to update their pod for.

How to configure MainThread checker for your own code?

There're some undocumented environment variables like MTC_SUPPRESSION_FILE which allows you to provide a list of classes, methods & selectors to exclude from the checker. An opposite way to what you're looking for. I checked (quickly) the libMainThreadChecker.dylib and can't find more of them.

Then I got a reply from an Apple engineer - there's no documented way. It can mean anything - there's no way or there's a way, but it's not documented1. He suggests to just use ...

dispatchPrecondition(condition: .onQueue(.main))

... at the beginning of your method.

1 The library is available at /Applications/Xcode-beta.app/Contents/Developer/usr/lib/libMainThreadChecker.dylib if anyone wants to dig in.

Main Thread Checker dylib injection

There's a number of ways you can do this: the easiest is probably setting the DYLD_INSERT_LIBRARIES environment variable to /Applications/Xcode.app/Contents/Developer/usr/lib/libMainThreadChecker.dylib. (You can do this from the "Arguments" tab of the Schemes editor.)

Another way would be to dynamically load it in code. Just put something like dlopen("/Applications/Xcode.app/Contents/Developer/usr/lib/libMainThreadChecker.dylib", RTLD_LAZY) early in your app's startup path.

You could also directly link against the library, but that would hardcode the library into your app, which I would not really recommend for something used largely for debugging.



Related Topics



Leave a reply



Submit