1

我正在尝试使用 FSEventStreamCreate 监听文件更改 - 虽然没有调用回调。以下是代码,应该按原样工作,但是?

提前致谢

#import "FileChangeController.h"
#include <CoreServices/CoreServices.h>

@implementation FileChangeController

- (id) init{
    self = [super init];
    if (self != nil) {
//        fm = [NSFileManager defaultManager];
//        images = [NSMutableArray new];
        [self initializeEventStream];
    }
    return self;
}

- (void) awakeFromNib{

}

void fsevents_callback(ConstFSEventStreamRef streamRef,
                       void *userData,
                       size_t numEvents,
                       void *eventPaths,
                       const FSEventStreamEventFlags eventFlags[],
                       const FSEventStreamEventId eventIds[])
{
    //FileChangeController *ac = (__bridge FileChangeController *)userData;
    NSLog(@"Test");
    size_t i;
    for(i=0; i<numEvents; i++){
        //[ac addModifiedImagesAtPath:[(NSArray *)eventPaths objectAtIndex:i]];
        //[ac updateLastEventId:eventIds[i]];
    }
}

- (void) initializeEventStream
{
    NSString *myPath = @"/Users/NameOfUser/Desktop/Untitled.js"; //NSHomeDirectory();
    NSArray *pathsToWatch = [NSArray arrayWithObject:myPath];
    void *appPointer = (__bridge void *)self;
    FSEventStreamContext context = {0, appPointer, NULL, NULL, NULL};
    NSTimeInterval latency = 3.0;

    stream = FSEventStreamCreate(NULL,
                                 &fsevents_callback,
                                 &context,
                                 (__bridge CFArrayRef) pathsToWatch,
                                 kFSEventStreamEventIdSinceNow, //[lastEventId unsignedLongLongValue],
                                 (CFAbsoluteTime) latency,
                                 kFSEventStreamCreateFlagUseCFTypes
                                 );

    FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    FSEventStreamStart(stream);
}

@end
4

2 回答 2

2

试试这个,参数有点改变,但它对我有用。

static void fsEventsCallback(ConstFSEventStreamRef streamRef, void *clientCallBackInfo, size_t numEvents, void *eventPaths, const FSEventStreamEventFlags eventFlags[], const FSEventStreamEventId eventIds[]);

- (void)initializeEventStream {
    NSArray *pathsToWatch = [NSArray arrayWithObject:@"/Users/NameOfUser/Desktop/Untitled.js"];
            FSEventStreamContext context;
            context.info = self;
            context.version = 0;
            context.retain = NULL;
            context.release = NULL;
            context.copyDescription = NULL;
            stream = FSEventStreamCreate(NULL, fsEventsCallback, &context, (CFArrayRef)pathsToWatch, kFSEventStreamEventIdSinceNow, 1.0, kFSEventStreamCreateFlagWatchRoot);
            FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
            FSEventStreamStart(stream);

}

void fsEventsCallback(ConstFSEventStreamRef streamRef, void *clientCallBackInfo, size_t numEvents, void *eventPaths, const FSEventStreamEventFlags eventFlags[], const FSEventStreamEventId eventIds[]){
 // Your callback handling here
}
于 2012-09-07T08:39:22.567 回答
-2
(void)initializeEventStream {
    NSArray *pathsToWatch = [NSArray arrayWithObject:@"/Users/NameOfUser/Desktop/Untitled.js"];
    FSEventStreamContext context;
    context.info = (__bridge void *)self;     // !!!
    context.version = 0;
    context.retain = NULL;
    context.release = NULL;
    context.copyDescription = NULL;
    stream = FSEventStreamCreate(
        kCFAllocatorDefault,                  // !!!
        fsEventsCallback,
        &context, 
        CFBridgingRetain(pathsToWatch),       // !!!
        kFSEventStreamEventIdSinceNow,
        1.0, 
        kFSEventStreamCreateFlagWatchRoot);
    FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
    FSEventStreamStart(stream);
}
于 2015-10-13T17:18:34.253 回答