0

I have a pipe in an iOS program and I want the main runloop (or any other runloop) to let me know when there is data to read...

So how can I add the file descriptor for the pipe to the runloop?

(I'm pretty sure under the hood the app must be running select/kqueue/poll/whatever to receive it's events, so it should just be a matter of getting the FD to that call, but I can't find relevant information on what the right API call is.)

1 Answers1

1

See the section Creating and Using a Dispatch I/O Channel in the Apple Documentation for how to do this using GCD, which is approximately the same thing. In brief, you want to create a dispatch_io channel, and queue up reads on that:

dispatch_io_t  channel =  dispatch_io_create(DISPATCH_IO_STREAM,
                                             fd,
                                             dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
                                             ^(int error) {

                                             });

dispatch_io_read(channel,
                 0,
                 1024,
                 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^(bool done, dispatch_data_t data, int error) {
                     // Code to execute when 1024 bytes become available
                 });

As part of the read handling queue, you should just restart the read request.

Alternatively, you can create a CFFileDescriptorRef and use that to create a CFRunLoopSourceRef.

David Berry
  • 40,941
  • 12
  • 84
  • 95
  • Also note that pipes on iOS are really only going to be useful to talk to yourself, since you can't fork other process and use them as IPC mechanisms. At that point, a better question might be whether or not a pipe is really the best solution to your problem. – David Berry May 22 '14 at 22:40
  • Thanks, I'll give this a try and mark this correct if it works. I am in fact talking to myself, trying to get data from a separate POSIX-oriented library compiled into the application. –  May 22 '14 at 22:43