使用 FSEventStreamCreate 创建的流为零
我想创建一个简单的事件流,以便在目录中发生某些更改时侦听事件。第一步是创建流,但我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用此代码:
另请查看此链接:监控文件更改使用文件系统事件 API。它向您展示了如何监视 Cocoa 应用程序中的文件更改。
Try this code instead:
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.