2013-01-18 38 views
1

我想避免運行計時器,並且想要在數據庫文件被修改時使用FSEventStreamCreate來通知應用程序。我通過在要檢查的路徑數組中包含NSHomeDirectory()來獲得應用程序的結果,但僅僅是因爲主目錄非常繁忙。如果我排除NSHomeDirectory(),或者由於某種原因機器不如我的繁忙,那麼應用程序不會經常將更改記錄到數據庫文件。使用FSEventStreamCreate跟蹤目錄中的文件系統事件不能正常工作,因爲我預計

以下代碼每次啓動一個調試會話時只能使用一次。

-(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

​​

更加適應任何想法?

回答

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); 
}