FSEvents를 사용하여 디렉토리를 모니터링하고 있으며 디렉토리가 변경 될 때마다 원래 FSEventStreamRef의 FSEventStreamContext로 전달한 블록을 호출합니다. 디렉토리 모니터링을 중지 할 때 블록을 어떻게 해제합니까? 참조 용으로 아래 코드를 작성하십시오. 기능이 예에서 void *
블록 포인터이다 info
포인터를 보유하고 해제하기위한OS X 및 FSEvents : FSEventStreamRef에 제공된 콜백 포인터를 어떻게 릴리스합니까?
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);
}
블록을 해제 할 필요가 없습니다. 적어도 나는 그것을 본 적이 없다. – trojanfoe