Snippet: Running Tasks in the Background While Updating the UI

This is just a very quick note about multithreading in iOS 4.0+. If you want to put a long-running task to the background but keep your users updated about the state of the task you can use this snippet.

// start background execution
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
      // do background work here (e.g. in a loop)
            
      // everytime you want to update the ui call this
      dispatch_async(dispatch_get_main_queue(), ^{
                
            // update your ui here .. e.g.
            [myUIProgressView setProgress:myProgress];
            // you need to provide these variables yourself of course ;)
                
      });
});

NIGHTNIGHT by DEDDY