objective-ccocoacfrunloop

Need to CFRunLoopRun() but want it unblocking


I have the following piece of code in a cocoa Application for OSX:

void *callbackInfo = NULL; // could put stream-specific data here.
            FSEventStreamRef stream;
            CFAbsoluteTime latency = 1.0; /* Latency in seconds */

            /* Create the stream, passing in a callback */
            stream = FSEventStreamCreate(NULL,
                                         &mycallback,
                                         callbackInfo,
                                         pathsToWatch,
                                         kFSEventStreamEventIdSinceNow, /* Or a previous event ID */
                                         latency,
                                         kFSEventStreamCreateFlagFileEvents//kFSEventStreamCreateFlagNone /* Flags explained in reference */
                                         );

            /* Create the stream before calling this. */
            FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(),kCFRunLoopDefaultMode);
            FSEventStreamStart(stream);



            CFRunLoopRun();

        }

This code gets notified about files system changes and reacts accordingly (the callback function is not in the snippet).

My problem now is that the CFRunLoopRun() is blocking. I.e. the further execution of the code stops. However, I'm looking for a possibility that I can start the observation of file system changes but also stop it again (e.g. from another object).

One option that I thought of would be to only start the loop for a second and check for a global variable afterwards. However, I usually don't like global variables....

Does anybody here have a nice and handy idea, how this could be solved? Would it be a good idea to buy the overhead and put the execution into a separate thread?

Thanks in advance! Norbert


Solution

  • A Cocoa application has an implicit run loop, so just delete CFRunLoopRun();

    Consider to use a dispatch queue via Grand Central Dispatch (GCD) rather than schedule the stream on a run loop