How to mark methods to be invoked only from the main thread in Obj-C?

Viewed 616

I'm experimenting with the new Xcode 9 Main Thread Checker

This is pretty neat. I'd like to now mark / annotate my Obj-C methods to be invoked only from the main thread so the Main Thread Checker can kick-in automatically when attached to the debugger. I can't find any way to do this.

2 Answers

The main problem with Main Thread Checker is that it can catch execution main-thread only code on a background thread when that reaches it i.e. on runtime.

The real solution would be to use static checking even before code is executed since sometimes is really hard to go trough all use cases in a non-trivial application.

More over, it is not quite an info (assert) that code is executing on a wrong thread while it will crash a moment later anyway.

I would like to see something like this in Swift: @mainThreadOnly and @isOnMainThread

and

if #mainThreadOnly && !#isOnMainThread { raise_warning }

Related