我正在使用 FSEvents 来监视目录,并且每当目录更改时,我都会调用一个块,该块最初传递到 FSEventStreamRef 的 FSEventStreamContext 中。在停止监视目录时如何释放块?下面的代码供参考。
void fsevents_callback(ConstFSEventStreamRef streamRef, void *clientCallBackInfo, size_t numEvents, void *eventPaths, const FSEventStreamEventFlags eventFlags[], const FSEventStreamEventId eventIds[]) {
void (^block)() = (__bridge void (^)())(clientCallBackInfo);
block();
}
- (FSEventStreamRef)startObserving:(NSString *)path block:(void(^)())block {
void *ptr = (void *)CFBridgingRetain(block); // NOTE: the block is retained
FSEventStreamContext context = { 0, ptr, NULL, NULL, NULL };
FSEventStreamRef stream = FSEventStreamCreate(NULL, fsevents_callback, &context, (__bridge CFArrayRef)@[path], kFSEventStreamEventIdSinceNow, 10, kFSEventStreamCreateFlagUseCFTypes | kFSEventStreamCreateFlagIgnoreSelf);
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
return stream;
}
- (void)stopObserving:(FSEventStreamRef)stream {
// HELP: the block should be released here. can I get it through FSEvents?
FSEventStreamStop(stream);
FSEventStreamInvalidate(stream);
FSEventStreamRelease(stream);
}