Sunday, August 1, 2010

NSOperation Xcode File Template

Although I'm generally averse to using a lot of template or boilerplate code, there are times when it's handy to have file template beyond those that Apple provides. Something I've done a fair amount of lately is to create NSOperation subclasses, and there's enough work involved with setting up an operation that I made an Xcode file template that contains all that setup work.

This template includes a delegate and a protocol and some private methods for communicating with the delegate. Now, when I have lots of NSOperation subclasses in a single project, I'll actually move much of this stuff to an abstract parent class or a category on NSOperation, but templates don't have any way of setting up dependencies, so I've made this self-contained and you can do your own refactoring.

I added this particular template under the Objective-C class icon so that it comes up as a new option under the Subclass of popup menu.

Screen shot 2010-08-01 at 1.53.48 PM.png

You can find the template files right here. The zip file contains the full path to install everything in the correct place, so if you want to install it so that you can use it from Xcode, you would use the following command:
unzip NSOperationTemplate.zip -d /

If you just unzip it regularly, you'll find the actual files nested several folders down as a result of the path information.

I've only touched one existing file, which is a property list that causes the new template to show up in Xcode's Subclass of dropdown menu. The rest are new files, so installing this shouldn't interfere with Xcode in any way. But caveat emptor. You do keep good backups, right?

Hopefully this will be useful to some of you. If you have suggestions for making it better, please let me know.



7 comments:

Malcolm Hall said...

Jeff, why aren't you using GCD?

Jeff LaMarche said...

I have to support back to 3.1 for some of my clients' applications. Using GCD would prevent those applications from running on 3.1 and also would prevent the code form running on the iPad. Using NSOperation and NSOperationQueue will work correctly on older versions and still gets most of the benefits of GCD since NSOperationQueue under under Snow Leopard and iOS 4 leverage GCD.

Even if I was able to drop support for 4.0, I'd likely still use NSOperation and NSOperationQueue for a lot of tasks. These are not performance sensitive tasks I'm working with right now, so the additional overhead of NSOperationQueue compared to straight GCD is meaningless. I'm a strong believer in using the highest-level of abstraction that meets my needs. For most of my concurrency tasks, NSOperationQueue meets my needs.

GCD is great, but it reminds me of inline assembly. Right in the middle of a method or function, BAM!, the abstraction changes drastically. It's worth doing when there's a good reason (like performance), but if NSOperationQueue is sufficient for the task, why clutter the code with GCD dispatch calls and bury the task logic in the middle of another class?

Billy Gray said...

Thanks for the template, Jeff. I think the second time I created an NSOperationQueue I was already thinking to myself, "a generator or template would be dang handy."

> but if NSOperationQueue is sufficient for the task, why clutter the code with GCD dispatch calls and bury the task logic in the middle of another class?

Agreed, I don't see the need for GCD for really simple tasks that can and should easily be chucked out onto an NSOperationQueue because they might temporarily hold up the main thread. Just because you're doing something off the main thread doesn't mean you need to get crazy with the hi-performance cheez whiz ;-)

Muhammad Adil said...

Hi Jeff, can you also write a tutorial on how to write your own templates?

Thanks.

Troy Gaul said...

FYI, another option for installation, which I tend to prefer, is to put the NSOperation Subclass.pbfiletemplate folder in ~/Library/Application Support/Developer/Shared/Xcode/File Templates/Cocoa Touch Class/.

Then when you use New File in Xcode, the dialog will contain an additional "User Templates" section on the left side with a "Cocoa Touch Class" entry which provides this template as a choice.

You can even put multiple folders in the Templates directory to group your user templates by purpose.

Halldór Sigurðsson said...

"Using NSOperation and NSOperationQueue will work correctly on older versions and still gets most of the benefits of GCD since NSOperationQueue under under Snow Leopard and iOS 4 leverage GCD."

NSOperationQueue under iOS4 doesn't use GCD according to the Apple docs.

From http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html%23//apple_ref/occ/cl/NSOperationQueue

"Note: In iOS, operation queues do not use Grand Central Dispatch to execute operations. They create separate threads for non-concurrent operations and launch concurrent operations from the current thread. For a discussion of the difference between concurrent and non-concurrent operations and how they are executed, see NSOperation Class Reference."

SEO Services Consultants said...

Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck! Web Design Company