Posts Tagged asynchronous

SSD versus HDD, Movie Frame Grabs and the importance of profiling

There was an e-mail to Apple’s cocoa-dev e-mail list that provoked a bit of discussion. The discussion thread starts with this e-mail to cocoa dev by Trygve.

Basically Trygve was wanting to get better performance from his code for taking frame grabs from movies and drawing those frame grabs as thumbnails to what I call a cover sheet. Trygve was using NSImage to do the drawing and was complaining that based on profiling his code was spending 90% of the time in a method called drawInRect.

Read the rest of this entry »

Tags: , , , , , , , ,

Asynchronous Design Patterns with Blocks, GCD and XPC

This is my notes/interpretation of WWDC 2012 Session 712.

Read the rest of this entry »

Tags: , , , ,

Don’t block the main thread by using gcd queues and Blocks

Or Asynchronous design patterns using gcd queue and Blocks

If you have cpu intensive work to do then you should not do that on the main thread. But you then need to remember to update the UI on the main thread. So you have one block that does the work not on the main thread, and this then contains another block which then updates the UI on the main thread. In the following example render thumbnails is done on the main thread.

// Main thread.
dispatch_queue_t queue;
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
void (^offLoadWorkBlock)(void) = 
  [self renderThumbnails];
  void (^updateUIOnMainThreadBlock)(void) = 
    [self.thumbnailView setNeedsDisplay:YES];
  dispatch_async(dispatch_get_main_queue(), updateUIOnMainThreadBlock);
dispatch_async(queue, offLoadWorkBlock);

Don’t allow too many background threads to become blocked.

If for example you want to download lots of images from the internet then you should use dispatch IO, this is demonstrated in the Asynchronous design patterns with blocks, GCD and ARC 2012 WWDC session video and is described about 40 minutes into the video.

dispatch_get_main_queue() is a useful method for getting the main queue.

Tags: , , , , ,