Posts Tagged queue

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: , , , , , , , ,

Blocks and Grand Central Dispatch in Practice

WWDC 2011 Session 308

Because this session is from 2011 I’m going to ignore most of the discussion on memory management for two reasons. Grand Central Dispatch (GCD) objects have since then been made into Objective-C objects which has changed the way they should be managed, plus Automatic Reference Counting (ARC) has changed significantly how we need to think about memory management for GCD objects and Blocks.

Please see:
Asynchronous design patterns with blocks gcd and xpc

Strong reference cycles and blocks

Please see this serious of articles by objc.io on concurrency and gcd plus more

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: , , , , ,