xcode9-betacompiler-optionsxcode9

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


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.


Solution

  • You can't (see update). But you could use an assert to do it manually (like PSPDFKit does for example)

    assert(Thread.isMainThread, "Method must be called from the MainThread only!")
    


    Update:

    There's no way. Tweet from an employee:

    unfortunately not; you cannot teach Main Thread Checker about you APIs (CC @kubamracek)

    by @zaks_anna (Program analysis @apple)


    Update2: There's an open radar being tracked by Apple, with ID#32659599. So hopefully, it will be implemented soon™.