使用 FSEventStreamCreate 创建的流为零

发布于 2024-12-08 22:56:57 字数 2356 浏览 1 评论 0原文

我想创建一个简单的事件流,以便在目录中发生某些更改时侦听事件。第一步是创建流,但我在使用 FSEventStreamCreate 函数创建流时收到错误。谷歌搜索这个错误是没有用的,我不明白错误在哪里。

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

我的代码与 苹果文档

无论如何,这是我的代码:

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

    NSLog(@"File Changed!");
}

- (void)applicationDidFinishLaunching:(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;
}

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)applicationDidFinishLaunching:(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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏剧牡丹亭 2024-12-15 22:56:57

尝试使用此代码:

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

}

另请查看此链接:监控文件更改使用文件系统事件 API。它向您展示了如何监视 Cocoa 应用程序中的文件更改。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文