使用FSEventStreamCreate跟踪目录中的文件系统事件无法正常工作

时间:2013-01-18 04:50:40

标签: objective-c macos

我想避免运行计时器,并希望在修改数据库文件时使用FSEventStreamCreate建议应用程序。我通过在要检查的路径数组中包含NSHomeDirectory()来获取应用程序中我想要的结果,但这只是因为主目录非常繁忙。如果我排除NSHomeDirectory()或由于某种原因机器不像我的那样繁忙,那么应用程序就不会经常对数据库文件进行更改。

以下代码每次启动调试会话时只能运行一次。

-(void) initializeEventStream
{

    NSString *databaseDirectoryPath = @"/Library/FileMaker Server/Data/Databases/development";
    NSString *homeDirectoryPath = NSHomeDirectory() ;

    NSArray *pathsToWatch = [NSArray arrayWithObjects:databaseDirectoryPath,  homeDirectoryPath, nil];
    void *appPointer = (void *)self;
    FSEventStreamContext context = {0, appPointer, NULL, NULL, NULL};
    NSTimeInterval latency = 3.0;

    self.fileSystemEventStreamRef = FSEventStreamCreate(NULL,
                                 &fsevents_callback,
                                 &context,
                                 (CFArrayRef) pathsToWatch,
                                 [self.lastEventIDNumber unsignedLongLongValue],
                                 (CFAbsoluteTime) latency,
                                 kFSEventStreamCreateFlagUseCFTypes
                                 );

    FSEventStreamScheduleWithRunLoop(self.fileSystemEventStreamRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    FSEventStreamStart(self.fileSystemEventStreamRef);
}

有关如何使FSEventStreamCreate

更具响应性的任何想法
NSString *databaseDirectoryPath = @"/Library/FileMaker Server/Data/Databases/development";

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:

-(void) initializeEventStream
{

    NSString *databaseDirectoryPath = @"/Library/FileMaker\ Server/Data/Databases/development";
    NSString *homeDirectoryPath = NSHomeDirectory() ;

    NSArray *pathsToWatch = [NSArray arrayWithObjects:databaseDirectoryPath,  homeDirectoryPath, nil];
    void *appPointer = (void *)self;
    FSEventStreamContext context = {0, appPointer, NULL, NULL, NULL};
    NSTimeInterval latency = 1.0;

    self.fileSystemEventStreamRef = FSEventStreamCreate(NULL,
                             &fsevents_callback,
                             &context,
                             (CFArrayRef) pathsToWatch,
                             kFSEventStreamEventIdSinceNow,
                             (CFAbsoluteTime) latency,
                             kFSEventStreamCreateFlagWatchRoot
                             );

    FSEventStreamScheduleWithRunLoop(self.fileSystemEventStreamRef, CFRunLoopGetCurrent(),     kCFRunLoopDefaultMode);
    FSEventStreamStart(self.fileSystemEventStreamRef);
}