gpt4 book ai didi

cocoa - 更改 fsevents 中的 pathsToWatch

转载 作者:行者123 更新时间:2023-12-03 17:36:02 25 4
gpt4 key购买 nike

我已经使用 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 数组,但这也导致了严重的访问错误。

有更好的方法吗?

最佳答案

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

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

关于cocoa - 更改 fsevents 中的 pathsToWatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7095985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com