Re: NSArrayController and threading
Re: NSArrayController and threading
- Subject: Re: NSArrayController and threading
- From: Joe Chan <email@hidden>
- Date: Wed, 22 Dec 2004 09:29:04 -0500
This approach is much simpler than my initial guess. The loading of
thumbnail works, however, the table is not getting updated with the
thumbnails until I scroll. Unfortunately, because binding eliminated
almost all the glue code, it also makes it very hard to debug why it's
not getting updated. I thought that once the didChange: is called, the
NSArrayController will trigger an update on the NSImageCell and should
redraw itself.
On Dec 20, 2004, at 6:26 PM, John C. Randolph wrote:
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
--
Joe Chan
Sun Microsystems, Inc.
Burlington, MA
Tel: (781) 442-0809
_______________________________________________
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