0

I keep crashing when doing:

[NSWindow orderFront:nil]

From a thread I spawned in my app. Is working with UI elements from thread not possible like GTK+?

Edit: oh goodness just found this: https://stackoverflow.com/a/11900929/1828637

So apparently I cant use NSWindow from another thread, so objc is out, is it possible to do multi thread window stuff with CoreFoundation instead? I have to do from thread so Im looking for alternative way

Community
  • 1
  • 1
Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • 1
    You can only work with UI elements on the main thread. – brandonscript Jul 25 '15 at 20:23
  • Thanks @remus does that apply to CoreFoundation too? – Noitidart Jul 25 '15 at 20:23
  • 1
    CF can be run on background threads so long as your notifications that trigger UI updates are configured to run on the main thread. Read more [here](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html#//apple_ref/doc/uid/10000057i) – brandonscript Jul 25 '15 at 20:29
  • Thanks very much @remus so CF is in the same situtation as objc huh? I have to use `dispatch_sync` and `dispatch_get_main_queue` for CF as well is this true? – Noitidart Jul 25 '15 at 20:30
  • 1
    There are other ways of doing it, but GCD takes all the guesswork out of it and makes it really simple as a developer to manage threads. – brandonscript Jul 25 '15 at 20:31
  • Thanks very much @remus can you please outline those other ways if you get a chance I am very much interested to learn them all and apply them. I actually teach others how to do stuff so this would help me show others :) – Noitidart Jul 25 '15 at 20:39
  • 1
    I already linked to the apple documentation in my second comment - check that out. Past that, just go start reading about it. – brandonscript Jul 25 '15 at 21:10
  • Thanks again @remus! – Noitidart Jul 25 '15 at 21:11

2 Answers2

2

UI interaction always has to be done on the main thread.

You can simply dispatch the code in question with GCD on the main thread:

dispatch_async( dispatch_get_main_queue(), ^(void)
{
   [NSWindow orderFront:nil];
});
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
2

You can only work with UI elements on the main thread.

I use GCD to ensure all UI activities are running on the correct thread:

dispatch_sync(dispatch_get_main_queue(), ^{
    // Do your UI updates!
});

See why:

In Cocoa Touch, the UIApplication i.e. the instance of your application is attached to the main thread because this thread is created by UIApplicatioMain(), the entry point function of Cocoa Touch. It sets up main event loop, including the application’s run loop, and begins processing events. Application's main event loop receives all the UI events i.e. touch, gestures etc.

Community
  • 1
  • 1
brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • I noticed that this is 10.6+ only, I was reading but I cant find the method to support <10.6 would you know by chance? – Noitidart Jul 25 '15 at 22:31
  • 1
    You'll have to use things like `performSelectorInBackground:withObject:` and `detachNewThreadSelector:toTarget:withObject:`. Read up on NSThread. – brandonscript Jul 25 '15 at 23:03