• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Do I need to use Distributed Objects or not?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Do I need to use Distributed Objects or not?


  • Subject: Re: Do I need to use Distributed Objects or not?
  • From: Chaz McGarvey <email@hidden>
  • Date: Wed, 9 Feb 2005 16:09:33 -0700

Hello:

On Feb 9, 2005, at 5:05 AM, Michael Becker wrote:

I need to do several thread-worthy little tasks in my application, e.g. loading data from a server or loading several images (much like iPhoto's import). I think I know the basic idea of threading in Cocoa but I was just wondering what exactly I need. I do not want to do it more complicated than necessary. Here are the general features I need:

1) the main thread needs to be informed when the detached thread has finished
2) the user should be able to cancel the detached thread (that's not a big problem, I just use a BOOL flag, right?)
3) the GUI has a progress indicator which should be in sync with whatever the detached thread does.

I have found DO to be riddled with "gotchas" and I try to avoid it wherever I can. If you don't feel like reinventing the wheel, then I have a solution that complies with all of your requirements:


http://www.brokenzipper.com/developer/threadedtask-0.3.tar.gz

ThreadedTask is a very lightweight class which manages the threading itself. It's very easy to use.

As my mechanism should work like iPhoto's image import, I will also need to update an NSImageView with the image that is currently being imported...

It would be simple to accomplish this, too. I would do something like this (coded in Mail.app):


/* Custom iteration method, called on a separate thread. */
- (int)imageLoadTask:(ThreadedTask *)theTask iteration:(unsigned)i
{
	MyImageContext *context = [theTask context];

	// load the next image.
	[context loadImageAtIndex:i];

	// tell the main thread this iteration is complete.
	[theTask reportProgress:i];

	// exit if this is the last image to load.
	if ( [context imageCount] == i + 1 ) {
		return 0;
	}

	// continue iterating.
	return 1
}

/* ThreadedTask delegate methods. */
- (void)threadedTask:(ThreadedTask *)theTask reportedProgress:(int)theProgress
{
MyImageContext *context = [theTask context];


	// put the image into a NSImageView or something.
	DisplayImage( [context imageAtIndex:theProgress] );
}

- (void)threadedTaskFinished:(ThreadedTask *)theTask
{
	NSLog( @"all done loading the images..." );
}


Good luck, Chaz McGarvey http://www.brokenzipper.com

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Do I need to use Distributed Objects or not? (From: Michael Becker <email@hidden>)

  • Prev by Date: Re: Folder Icons In Sidebar?
  • Next by Date: Re: Threading...
  • Previous by thread: Re: Do I need to use Distributed Objects or not?
  • Next by thread: Re: Do I need to use Distributed Objects or not?
  • Index(es):
    • Date
    • Thread