Shawn,

I'm using KVO in this case as a means to only do certain processing when a thread really exits. I start my Core Audio thread like this:

- (void)start
{
streamerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startInternal) object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                                                
  selector:@selector(threadStopped)
                                                                                
  name:NSThreadWillExitNotification
                                                                                
  object:streamerThread];
        [streamerThread start];
        
MagnatuneAppDelegate *appDelegate = (MagnatuneAppDelegate *) [[UIApplication sharedApplication] delegate];
        appDelegate.playbackThreadFinished = false;
}

... so when the thread stops (or is as close to stopping as I can get -- I figure if it's about to call exit(), that's good enough), this happens:

- (void)threadStopped
{
        NSLog(@"*** streamer thread has stopped ***");
        
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        
MagnatuneAppDelegate *appDelegate = (MagnatuneAppDelegate *) [[UIApplication sharedApplication] delegate];
        appDelegate.playbackThreadFinished = true;
}

... and I have this KVO handler:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                                                change:(NSDictionary *)change 
context:(void *)context
{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSLog(@"observeValueForKeyPath -- current thread ID: %x, keypath: %s", [NSThread currentThread], [keyPath UTF8String]);

        if ([keyPath isEqualToString:@"isPlaying"])
        {
                .
                .
                .
        }
        else if ([keyPath isEqualToString:@"playbackThreadFinished"])
        {
                if (playbackThreadFinished)
                {
// do special stuff that should only happen once the core audio thread has finished
                        // nothing UI-related goes on here

                        [self do_something];
                        .
                        .
                        .
                }

                [pool release];
                return;
        }
        else if ([keyPath isEqualToString:@"donePlaying"])
        {
                .
                .
                .
        }
        else if ([keyPath isEqualToString:@"nextSong"])
        {
                .
                .
                .
        }
        
        [pool release];
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}

So I know the KVO handler gets called on the thread that changed the value. Are you saying that do_something should happen on the main thread i.e. performSelectorOnMainThread:@selector(do_something)? Should I make sure _anything_ that happens in this KVO handler is dispatched to the main thread in that way?


On Oct 22, 2008, at 2:00 PM, Shawn Erickson wrote:

On Wed, Oct 22, 2008 at 1:46 PM, John Zorko <[EMAIL PROTECTED]> wrote:

Bill,

Alas, I found it 10 minutes after I posted the message *sigh* ... I forgot
the autorelease pool in my override of
-observeValueForKeyPath:ofObject:change:contet: ... the console message is
gone now :-)


Humm that has me concerned... it usually isn't a good idea for KVO to
be fired from a secondary thread (since often UI, etc. can be involved
and often that isn't safe to be doing from a secondary thread). You
likely should be bouncing KVO triggering operations to the main
thread.

-Shawn


Regards,

John

Falling You - exploring the beauty of voice and sound
http://www.fallingyou.com











_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to