简体   繁体   中英

How to configure MainThread checker for your own code?

I have a class method that I want to always execute in the main thread.

Is there a way I could configure XCode Main Thread checker to raise a "purple warning" if that method is called on a background thread (just like it does for UI-related methods)?

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

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