更改 fsevents 中的 pathsToWatch
我已经使用 fsevents 建立了一个观察者机制。其要点是每次在文件夹 X 中创建文件时,我都希望运行一个函数。它现在运行良好,但我需要能够更改它正在监视的路径。这是设置代码:
void *appPointer = (void *)self;
NSString *myPath = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] stringForKey:@"FolderPath"];
NSArray *pathsToWatch = [NSArray arrayWithObject:myPath];
FSEventStreamContext context = {0, appPointer, NULL, NULL, NULL};
NSTimeInterval latency = 1.0;
stream = FSEventStreamCreate(NULL,
&fsevents_callback,
&context,
(CFArrayRef) pathsToWatch,
[lastEventId unsignedLongLongValue],
(CFAbsoluteTime) latency,
kFSEventStreamCreateFlagUseCFTypes
);
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
我首先尝试停止 FSEventStream,然后再次启动它,但再次调用 FSEventStreamCreate
时,我收到 exc_bad_access
。
还尝试动态调整 pathsToWatch
数组,但这也导致了严重的访问错误。
有更好的方法吗?
I've set up a watcher mechanism using fsevents. The gist of it is every time a file is created in folder X, I want a function to run. It's running fine now, but I need to be able to change the path it is watching. Here's the setup code:
void *appPointer = (void *)self;
NSString *myPath = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] stringForKey:@"FolderPath"];
NSArray *pathsToWatch = [NSArray arrayWithObject:myPath];
FSEventStreamContext context = {0, appPointer, NULL, NULL, NULL};
NSTimeInterval latency = 1.0;
stream = FSEventStreamCreate(NULL,
&fsevents_callback,
&context,
(CFArrayRef) pathsToWatch,
[lastEventId unsignedLongLongValue],
(CFAbsoluteTime) latency,
kFSEventStreamCreateFlagUseCFTypes
);
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
I first tried just stopping the FSEventStream, then starting it up again, but I get an exc_bad_access
when calling FSEventStreamCreate
again.
Also tried adjusting the pathsToWatch
array on the fly, but that also caused a bad access error too.
Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是事件流仍然是预定的;阻止它还不够。我是这样做的:
基本上与我在问题中列出的内容相同,但也使用
FSEventStreamUnscheduleFromRunLoop
。The problem was that the event stream was still scheduled; stopping it wasn't enough. Here's how I did it:
Basically the same thing as I listed in my question, but with
FSEventStreamUnscheduleFromRunLoop
too.