Re: NSArrayController and threading
Re: NSArrayController and threading
- Subject: Re: NSArrayController and threading
- From: "John C. Randolph" <email@hidden>
- Date: Mon, 20 Dec 2004 15:26:21 -0800
On Dec 19, 2004, at 12:58 PM, Joe Chan wrote:
I have a general question about threading and NSArrayController. For
the model objects that I need to display in a NSTableView, one of the
columns is for thumbnails (and the model object has a thumbnail key
that returns an NSImage). Since loading a bunch of thumbnails can take
a while, I'm planning to have the model object returns a placeholder
NSImage, and then go off and fetch the thumbnail in a the thread. My
question is how to do so safely. There is no indication that
NSArrayController is thread safe, and having another thread calling
[obj setThumbnail] after the thumbnail is loaded (which trigger all
the KVO stuff in that thread) is probably a bad idea. One idea I have
is to set up a work queue and and NSTimer in the main thread to
retrieve the thumbnail as they are loaded. Any better ideas?
Hmm... I think I'd do something like this:
(In your model object)
- (void) loadThumbnails //dispatched with
-detachNewThreadSelector:toTarget:withObject:
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
NSMutableIndexSet *loadedIndexes = [NSMutableIndexSet indexSet];
BOOL done = NO;
while (!done)
{
// load some thumbnails, note their indexes in loadedIndexes
...
if (got the last thumbnail)
done = YES;
[self performSelectorOnMainThread:@selector(gotThumbnailsAtIndexes:)
withObject:loadedIndexes waitUntilDone:NO];
}
[myPool release];
}
- gotThumbnailsAtIndexes:(NSIndexSet *) indexes // invoked on main
thread
{
[self didChange:NSKeyValueChangeReplacement valuesAtIndexes:indexes
forKey:@"thumbnail"];
}
You may want to message the main thread after each thumbnail comes in,
or you may want to accumulate a few thumbnails and then get your
notifications out. -jcr
John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
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