views:

85

answers:

1

I have a few Cocoa UI elements with outlet connexions to an object instantiated within an NSView object, which is in turn put there by an NSViewController. These elements, a definite progress bar and a text label, are not updating: the progress bar is dead and empty despite having its value change constantly, the text label does not unhide through [textLabel setHidden:NO], the text label does not change its string.

What I know:

  • There's no difference between binding values and setting them in code. Nothing changes either way.
  • I've checked outlet connections. They're all there.
  • I've tried [X displayIfNeeded], where X has been the UI objects themselves, the containing NSView, and the main window. No difference.
  • [progressBar setUsesThreadedAnimation:YES] makes no difference. Interestingly, if I look at progressBar mid-program, _threadedAnimation is still NO.
  • The object holding all these outlets and performing an import operation is in an NSOperationQueue owned by the NSViewController object.

Thanks!

EDIT: As suggested, I called [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithInt:myObject] waitUntilDone:NO]. (I've also tried waitUntilDone:YES.) It's still not updating. The debugger clearly shows updateProgress: taking place in the main thread, so I don't know what's missing.

A: 

All NSOperation tasks are performed in separate threads. You are not allowed to modify the UI from a separate thread as AppKit is not thread-safe. You will need to refactor your code so that any calls that modify the user interface are performed on the main thread using the method:

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
Rob Keniger
Is it not OK to call a selector defined in an NSOperation? e.g., [self performSelectorOnMainThread:@selector(updateProgress:) withObject:myObject waitUntilDone:YES]; I'm trying this and I get no improvement. I'd have this selector in my app controller, but, well...I don't know how to link it to this object. :/
spamguy