Posts Tagged Blocks

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

Replacement for NSTimer

I’ve been looking around for a NSTimer replacement. Mostly to avoid needing to make sure the timer is running on a runloop and that your running the timer in a suitable mode. All of the replacements use gcd and queues.

I’ve found a number of possible replacements, but one in particular looks like the one to go for. The three others use the main thread and I want to get away from the main thread. It is easy enough to send a update ui message back to the main thread once the required work has been done.

I like the simplicity of this NSTimer replacement, it doesn’t require a new class instead everything is wrapped up nicely in a block and it will keep on firing until the object that it fires the doSomething message to no longer exists. What I don’t like is that the code is run on the main thread.

This nice simple timer class is also good, but it also runs on the main thread.

Finally I come to MSWeakTimer. I’m in the process of including it in my project so I’ll report back when I’m done. It is more complicated than the other timers but it is not dependent on the runloop or the main queue.

Report Back

MSWeakTimer seems to be working as expected. I needed to make a couple of small changes to get rid of warnings. I’ve got no warnings in my current project and I’m hoping to keep it that way.

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

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

Strong reference cycles and blocks

If a block refers to a member of an object then the blocks takes ownership of the object. If the object has also taken ownership of the block to keep it alive for at least the lifetime of the object then this creates a strong reference cycle. Possible solutions are:

  • Create a local variable within the scope where the blocks is implemented or have something like a cancel method on the object that nils the member that is the one that owns the block.
  • Or you can use weak references in the scope where the block is implemented and then assign a strong reference within the block scope and check to see if the reference is nil or not before deciding on what work to do within the block.

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