• 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
NSFileHandle stdin sends ∞ notifications of 0 bytes available
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSFileHandle stdin sends ∞ notifications of 0 bytes available


  • Subject: NSFileHandle stdin sends ∞ notifications of 0 bytes available
  • From: Jerry Krinock <email@hidden>
  • Date: Thu, 12 Jun 2014 15:24:59 -0700

Hello,

I need to develop a tool which processes stdin as it arrives from its parent process.  The following code works as expected when it is in invoked with no stdin, that is…

Air2:Debug jk$ ./MyTool

But if I pipe some initial stdin to it, like this

Air2:Debug jk$ echo Hello | ./MyTool

after the initial notification and processing of “Hello”, the notification-handling block infinitely receives repeated, different notifications, for which -availableData returns an empty data object.

I can’t understand and cannot work around it.  Why am I getting infinitely repeated notifications when there is no data available?

Thanks!

Jerry

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        /* -[NSFileManager waitForDataInBackgroundAndNotify] docs say I need
         an "active run loop".  I don't know what they mean by "active".
         Maybe this will help (it didn't). */
        NSDate* verySoon = [NSDate dateWithTimeIntervalSinceNow:0.01] ;
        [[NSRunLoop mainRunLoop] runUntilDate:verySoon] ;

        NSFileHandle* input = [NSFileHandle fileHandleWithStandardInput] ;
        [input waitForDataInBackgroundAndNotify] ;
        NSNotificationCenter* dc = [NSNotificationCenter defaultCenter] ;
        [dc addObserverForName:NSFileHandleDataAvailableNotification
                        object:input
                         queue:nil
                    usingBlock:^(NSNotification* note) {
                        NSFileHandle* input ;
                        input = [NSFileHandle fileHandleWithStandardInput] ;
                        NSData* data = [input availableData]  ;
                        NSLog(@"Got noted of %ld bytes", (long)[data length]) ;
                        if ([data length] > 0) {
                          NSFileHandle* output ;
                          output = [NSFileHandle fileHandleWithStandardOutput] ;
                          [output writeData:[NSData dataWithBytes:"--> "
                                                           length:4]] ;
                          [output writeData:data] ;
                        }

                        [input waitForDataInBackgroundAndNotify] ;
                    }] ;


        /*SSYDBL*/ NSLog(@"Running run loop") ;

        [[NSRunLoop mainRunLoop] run] ;
    }

    return 0 ;
}


_______________________________________________

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


  • Follow-Ups:
    • Re: NSFileHandle stdin sends ∞ notifications of 0 bytes available
      • From: Greg Parker <email@hidden>
  • Prev by Date: Re: document inexplicably becomes locked and fails to save under sandboxing
  • Next by Date: Re: NSFileHandle stdin sends ∞ notifications of 0 bytes available
  • Previous by thread: Drop on a NSCollectionView
  • Next by thread: Re: NSFileHandle stdin sends ∞ notifications of 0 bytes available
  • Index(es):
    • Date
    • Thread