1

我想避免运行计时器,并希望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";

?

4

1 回答 1

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);
}
于 2013-09-11T16:36:43.130 回答