0

I have some long running tasks (IE retrieving complex data from a DB) that I need to run any time my view appears. I know for performance reasons I want to take my long running tasks off the main thread. I also know that I have to get back ON the main thread to do anything UI related. I'm trying to figure out the right GCD pattern for accomplishing this. From what I see in the GCD docs, this seems to be the "right" way to do things, but I'm curious if people found another pattern to be more useful:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

     // Add long running code for off the UI thread here.  

     dispatch_async(dispatch_get_main_queue(), ^{

          // Add UI updating code here back on the UI thread.

     });
});

I'm basically saying asynchronously (IE don't block the UI thread) go to a separate thread, do some stuff, then get back to the main thread and update the UI.

Mike
  • 1,112
  • 1
  • 13
  • 20

1 Answers1

1

Your approach is good. The only disadvantage I can see is that you don't have any way of canceling the long running operation. This can lead to memory and other resource congestion.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200