0

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); 
} 
+0

블록을 해제 할 필요가 없습니다. 적어도 나는 그것을 본 적이 없다. – trojanfoe

답변

0

FSEventStreamContext 멤버 변수를 갖는다.

Apple's FSEvents reference를 통해 :

retain 
    The callback used retain the info pointer. This can be NULL. 

release 
    The callback used release a retain on the info pointer. This can be NULL. 

첫째, 유지한다. 블록을 FSEventStreamContext에 대해 void *으로 전송해야하므로 startObserving: 메소드에서 CFBridgingRetain()을 계속 사용하는 것이 좋습니다. 유지 콜백 기능이 필요하지 않습니다.

릴리스의 경우,이 콜백 함수를 시도 :

void release_callback(const void *info) { 
    CFBridgingRelease(info); 
} 

그런 다음에 FSEventStreamContext 선언을 변경해보십시오 : stopObserving:가 호출 될 때 블록을 해제해야

FSEventStreamContext context = { 0, ptr, NULL, release_callback, NULL }; 

.

+0

감사합니다. 나는 FSEventStreamContext의 두 변수에 전혀 관심을 기울이지 않았다. – Steveo

+0

여러분, 제게 코드가 어떻게 공개되는지 알려주시겠습니까 ?? – Sid