我正在尝试使用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
答案 0 :(得分:2)
尝试这个,params有点改变,但这对我有用。
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
}
答案 1 :(得分:-1)
(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);
}