In the documentation for NSApplicationMain, it says:
Creates the application, loads the main nib file from the application’s main bundle, and runs the application. You must call this function from the main thread of your application [...].
The "main thread" obviously refers to the first thread of the program, where main(argc, argv)
starts. A quick look through the NSThread documentation reveals + (BOOL)isMainThread
, which can be used to determine whether the current thread is the "main" one or not. I ran some tests: this method works regardless of whether NSApplicationMain
has been called yet.
My question has two (somewhat related) parts:
NSApplicationMain
?Here is a good place to study NSApplicationMain by following a re-implementation of the function. NSApplicationMain must be called from the main thread primarily because
So pretty much, since Cocoa calls things in the main thread, and the UI needs to all be run in the same thread, you need to work within main thread for anything touching UI, including NSApplicationMain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With