Re: Sublclassing NSThread
Re: Sublclassing NSThread
- Subject: Re: Sublclassing NSThread
- From: Jean-Daniel Dupas <email@hidden>
- Date: Wed, 17 Dec 2008 10:32:11 +0100
Le 17 déc. 08 à 04:27, Michael Ash a écrit :
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
We already discuss this issues, and I agree with previous post that
said you misuse it:
http://www.cocoabuilder.com/archive/message/cocoa/2008/11/26/223819
_______________________________________________
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