NSNotification and NSView problem???


hello everyone,
freshman, , trying make mac application upgrade device, , trouble now. here's question:

project has main window, binds appdelegate controller; , has 2 threads: parent-thread shows view, child-thread b upgrade , on.

between 2 threads, use notification center achieve inter-process communication. when thread b finished doing something, sent notification thread a, , thread change on view, such change values of textfield , on.

question is: view shows correctly, , other times fails show changed ones. how can deal ?
 

don't use nsnotificationcenter communicating between threads. notifications received on same thread in sent, in other words, it's not doing @ want to. nsnotificationcenter more geared towards implementing observer design pattern, not threading.

http://developer.apple.com/library/mac/#documentation/cocoa/reference/foundation/classes/nsnotificationcenter_class/reference/reference.html

you use grand central dispatch schedule block execution on thread a's dispatch queue instead. grand central dispatch allow thread b.

code:
 /* in thread b */ dispatch_async(dispatch_get_main_queue(), ^{   /*   ... code want execute on thread ... */ }); 
alternatively can use nsobject's method perform selector on main thread.

http://developer.apple.com/library/mac/#documentation/cocoa/reference/foundation/classes/nsobject_class/reference/reference.html

code:
 - (void)performselectoronmainthread:(sel)aselector withobject:(id)arg waituntildone:(bool)wait modes:(nsarray *)array 
both methods going work queuing message on application's run loop on main thread execution @ later time.

there might other methods accomplishing well. anybody?
 


Forums Macs Mac Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

Comments

Popular posts from this blog

Mail will not Quit when shutting down

MacBook Air 2011 Reviews

What's the max amount of RAM an iMac can have to run BootCamp & Parallels Windows 7?