开发者

Stream created with FSEventStreamCreate is nil

I want to create a simple event stream in order to listen events when some changes ocurre in a directory. The first step is the creation of the stream, but I receive an error in the creation using FSEventStreamCreate function. Googling this error was useless, I can not understand where is the error.

(CarbonCore.framework) FSEventStreamCreate: _FSEventStreamCreate: ERROR: (CFStringGetTypeID() != CFGetTypeID(cfStringRef)) (i = 0)

My code is quite similar to the one present in the apple documentation

Anyway, this is my code:

static void gotEvent(ConstFSEventStreamRef stream, 
                 void *clientCallBackInfo, 
                 size_t numEvents, 
                 void *eventPathsVoidPointer, 
                 const FSEventStreamEventFlags eventFlags[], 
                 const FSEventStreamEventId eventIds[]
                 ) {

    NSLog(@"File Changed!");
}

- (void)appl开发者_如何学CicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSString *fileInputPath = @"/tmp/mamma/ciao.txt";
    FSEventStreamRef stream = [self eventStreamForFileAtPath: fileInputPath];
}

- (FSEventStreamRef) eventStreamForFileAtPath: (NSString *) fileInputPath
{
    if (![[NSFileManager defaultManager] fileExistsAtPath:fileInputPath]) {
        @throw [NSException exceptionWithName: @"FileNotFoundException"
                                   reason:@"There is not file at path specified in fileInputPath"
                                 userInfo: nil];
    }
    CFStringRef fileInputDir = (CFStringRef)[fileInputPath stringByDeletingLastPathComponent];
    CFArrayRef pathsToWatch = CFArrayCreate(NULL, (const void **) fileInputDir, 1, NULL);
    void *callbackInfo = NULL;
    CFAbsoluteTime latency = 3.0; /* Latency in seconds */

    FSEventStreamRef stream = FSEventStreamCreate(
                                                 NULL, 
                                                 &gotEvent, 
                                                 callbackInfo, 
                                                 pathsToWatch, 
                                                 kFSEventStreamEventIdSinceNow, 
                                                 latency, 
                                                 kFSEventStreamEventFlagNone
                                                 );

    return stream;
}


Try this code instead:

- (FSEventStreamRef) eventStreamForFileAtPath: (NSString *) fileInputPath  {

   if (![[NSFileManager defaultManager] fileExistsAtPath:fileInputPath]) {
       @throw [NSException exceptionWithName:@"FileNotFoundException"
                                      reason:@"There is not file at path specified in fileInputPath"
                                    userInfo:nil];
   }

   NSString *fileInputDir = [fileInputPath stringByDeletingLastPathComponent];

   NSArray *pathsToWatch = [NSArray arrayWithObjects:fileInputDir, nil];

   void *appPointer = (void *)self;

   FSEventStreamContext context = {0, appPointer, NULL, NULL, NULL};

   NSTimeInterval latency = 3.0;

   FSEventStreamRef stream = FSEventStreamCreate(NULL,
                                                  &gotEvent,                                 
                                                  &context,                                 
                                                  (CFArrayRef) pathsToWatch,                                 
                                                  kFSEventStreamEventIdSinceNow,                                 
                                                  (CFAbsoluteTime) latency,                                 
                                                  kFSEventStreamCreateFlagUseCFTypes                                 
                                                  );

   FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
   FSEventStreamStart(stream);

   return stream;

}

Also take a look at this link: Monitoring File Changes with the File System Events API. It shows you how to monitor file changes in your Cocoa application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜