views:

82

answers:

1

Hi,

[Note: The title may be less than accurate, I didn't know how else to phrase it] For some reason NSFileHandle's readInBackground didn't work so I resorted to SCEvents, a Cocoa wrapper around Mac OS X's FSEvents API. I have a separate class called "EventListener" that handles all the SCEvents stuff.

It has these methods:

- (void)setupEventlistener
{
    SCEvents *events = [SCEvents sharedPathWatcher];

    [events setDelegate:self];

    NSMutableArray *paths = [NSMutableArray arrayWithObject:NSHomeDirectory()];
    NSMutableArray *excludePaths = [NSMutableArray arrayWithObject:[NSHomeDirectory() stringByAppendingString:@"/Downloads"]];

    [events setExcludedPaths:excludePaths];
    [events startWatchingPaths:paths];
}
- (void)pathWatcher:(SCEvents *)pathWatcher eventOccurred:(SCEvent *)event
{
    NSLog(@"%@", event);
}

( I got these methods directly from the SCEvents example app, once I get this to work I plan to change it for my own purposes )

Then in the applicationDidFinishLaunching method of my main app delegate class I have this:

EventListener *events = [[EventListener alloc] init];
[events setupEventlistener];

Which initializes the listener. Now, after allocing it and calling the setupEventListener class, everything works fine. Changes inside the home folder are logged into the debugger console as they should be. I have another method called format: that runs some shell scripts . The issue is, when the format method is running the event listener stops working. Any changes to the home folder are NOT logged. This problem only happens with the format: method. With all other methods the event listener works fine.

I'm not sure what the problem is. Thanks

+2  A: 

I have another method called format: that runs some shell scripts. The issue is, when the format method is running the event listener stops working. Any changes to the home folder are NOT logged.

This was probably the same reason why -readInBackgroundAndNotify: didn't work, either.

Specifically, notification mechanisms typically don't work unless you let the event loop (of the thread to which the notifications are targeted) run. In some cases, if you block long enough, notifications will be lost.

bbum
Is there a solution to this?
macatomy
Yes: Get back to the run loop.
Peter Hosey
Sorry..I'm not sure what that means (I only started Object Oriented Programming, Cocoa, and Objective-C a few months ago). I did find this page: http://www.cocoadev.com/index.pl?RunLoop And now I understand what the run loop is, but how do I get back to it after the shell script has been started? Thanks
macatomy
Yes, don't block the runloop.
bbum
So then basically create a new loop for the shell script and leave the run loop unblocked?
macatomy
That would be one way; new loop / thread. It'd be the wrong way, though. NSTask is already quite adept at spawning processes and handing the output back to you have NSFileHandle without you having to do any threading.
bbum
I just tried creating a new NSOperation and running the shell script on that. However I have the same issue as before. And I'm not using NSTask to run the shell script, I'm using AuthorizationExecuteWithPrivileges.
macatomy
PCWiz: When one of your actions or (via bindings) property accessors is called, you're ultimately being called from the run loop. So all you have to do is finish whatever chunk of work you have to do (e.g., setting up your event listener, or opening the file and sending the handle a `readInBackgroundAndNotify` message) and return.
Peter Hosey
Here's what I have right now in the format: method: http://pastebin.ca/1542495 Basically the event listener gets set up, then the authorization class gets alloced, and the script is run with AuthorizationExecuteWithPrivileges. What would I have to change in there to make it not block the runloop?
macatomy
I figured out my problem. It had something to do with a sheet panel that was opened directly before the build process. Once I disabled the sheet panel everything seemed to work fine
macatomy