Tuesday, October 16, 2012

Updating UI by the background thread

As simple as this:

 // Add to top of file
 #import <dispatch/dispatch.h>
 
 if ([NSThread isMainThread])
 {
  [self MyMethodName];
 }
 else
 {
  dispatch_sync(dispatch_get_main_queue(), ^{
   //Update UI in UI thread here
   [self MyMethodName];
  });
 }
 
Reference:
http://www.ios-developer.net/iphone-ipad-programmer/development/threads/updating-ui-controls-on-background-threads
http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial

No comments:

Post a Comment