更改 fsevents 中的 pathsToWatch

发布于 2024-11-29 23:37:28 字数 1141 浏览 0 评论 0原文

我已经使用 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 技术交流群。

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

发布评论

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

评论(1

病女 2024-12-06 23:37:28

问题是事件流仍然是预定的;阻止它还不够。我是这样做的:

- (void)initializeEventStream {
    void *appPointer = (void *)self;
    FSEventStreamContext context = {0, appPointer, NULL, NULL, NULL};
    NSTimeInterval latency = 1.0;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *pathsToWatch = [NSArray arrayWithObject:[defaults stringForKey:@"SomeKey"]];

    stream = FSEventStreamCreate(NULL, 
                                 &fsevents_callback, 
                                 &context, 
                                 (CFArrayRef) pathsToWatch, 
                                 [lastEventId unsignedLongLongValue], 
                                 (CFAbsoluteTime) latency, 
                                 kFSEventStreamCreateFlagUseCFTypes
                                 );
    FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    FSEventStreamStart(stream);
}

- (void)stopEventStream {
    FSEventStreamStop(stream);
    FSEventStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
}

基本上与我在问题中列出的内容相同,但也使用 FSEventStreamUnscheduleFromRunLoop

The problem was that the event stream was still scheduled; stopping it wasn't enough. Here's how I did it:

- (void)initializeEventStream {
    void *appPointer = (void *)self;
    FSEventStreamContext context = {0, appPointer, NULL, NULL, NULL};
    NSTimeInterval latency = 1.0;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *pathsToWatch = [NSArray arrayWithObject:[defaults stringForKey:@"SomeKey"]];

    stream = FSEventStreamCreate(NULL, 
                                 &fsevents_callback, 
                                 &context, 
                                 (CFArrayRef) pathsToWatch, 
                                 [lastEventId unsignedLongLongValue], 
                                 (CFAbsoluteTime) latency, 
                                 kFSEventStreamCreateFlagUseCFTypes
                                 );
    FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    FSEventStreamStart(stream);
}

- (void)stopEventStream {
    FSEventStreamStop(stream);
    FSEventStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
}

Basically the same thing as I listed in my question, but with FSEventStreamUnscheduleFromRunLoop too.

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