Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am reading data from a NSFileHandle (from a NSPipe) using a readabilityHandler block:

fileHandle.readabilityHandler = ^( NSFileHandle *handle ) {
     [self processData: [handle availableData]];
}

This works fine, I get all the data I expect fed to my processData method. The problem is that I need to know when the last chunk of data was read. availableData should return an empty NSData instance if it reached end-of-file, but the problem is that the reachability handler is not called again on EOF.

I can’t find anything about how to get some kind of notification or callback on EOF. So what am I missing? Is Apple really providing an asynchronous reading API without an EOF callback?

By the way, I cannot use the runloop based readInBackgroundAndNotify method since I don’t have a runloop available. If I cannot get this to work with the NSFileHandle API I probably will directly use a dispatch source to do the IO.

share|improve this question
    
Are you sure all write ends of the pipe have been closed? Regarding run loops, all threads will create a run loop on demand and you can run one yourself. – Ken Thomases Apr 6 '13 at 14:20
    
Yes, the pipe is properly closed. I did some more experimenting and the same thing also happens with other kinds of NSFileHandle instances. When reading from standard input or a regular file there also is no notification for EOF. – Sven Apr 6 '13 at 14:55
up vote 1 down vote accepted

I'm afraid you're out of luck doing this with NSFileHandle if you can't use readInBackgroundAndNotify.

Two solutions I see:

  1. Create a runloop and then use readInBackgroundAndNotify.
  2. Roll your own implementation using dispatch_io_*
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.