Re: Sublclassing NSThread
Re: Sublclassing NSThread
- Subject: Re: Sublclassing NSThread
- From: "Michael Ash" <email@hidden>
- Date: Tue, 16 Dec 2008 22:27:57 -0500
On Tue, Dec 16, 2008 at 5:19 PM, Bradley S. O'Hearne
<email@hidden> wrote:
> All,
>
> Thanks to everyone for the replies. In my code, I made an error -- overrode
> the start method rather than the main method. After I overrode the start
> method, everything worked great. Without trying to get too specific on the
> actual issue, the more general thrust of my original question was getting at
> encapsulating a piece of functionality that needs asynchronous execution
> (i.e. within a separate thread from the main thread) within an NSThread
> subclass. The reason this is useful is that rather than have this code
> scattered within an application which needs it, I can instead make a generic
> utility class out of it (which I've now done) and can reuse it anywhere. In
> my case, I was creating code to asynchronously load a large number of remote
> images (located on a server) within my application.
This design does not make sense to me. By subclassing NSThread you're
making it part of your public interface; in essence, you declare "I am
a thread!" to the world. But threadedness should be an implementation
detail, not part of the interface. The interface you present should
simply be "I am asynchronous." That you achieve this using a thread
doesn't matter to the outside world.
So instead of subclassing NSThread, subclass NSObject, then simply use
NSThread in your code to invoke a method from your object on a
separate thread. This gives you a clean design with maximum separation
of concerns. If you should decide to change how it works later on, for
example by using NSOperationQueue or asynchronous URL loading, you can
easily do so without fear of breaking your clients.
And before you go off using NSOperationQueue, you should be aware that
it's broken on Leopard, as described in this thread:
http://www.cocoabuilder.com/archive/message/cocoa/2008/10/30/221452
If you still like the idea of NSOperationQueue but don't want to use
it because of that, you may be interested in this replacement (which,
in the interest of full disclosure, I should tell you that I wrote):
http://www.rogueamoeba.com/utm/2008/12/01/raoperationqueue-an-open-source-replacement-for-nsoperationqueue/
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden