Objective-c FSEventStreamCreate [英] Objective-c FSEventStreamCreate

查看:99
本文介绍了Objective-c FSEventStreamCreate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FSEventStreamCreate侦听文件更改-尽管未调用回调.以下是代码,应该可以按原样工作,但是?

I am trying to listen on a file change using FSEventStreamCreate - the callback is not called though. Following is the code, and should have worked as is, but?

预先感谢

#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

推荐答案

尝试一下,参数有所变化,但是它对我有效.

try this, params are a bit changed, but it's working for me this way.

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
}

这篇关于Objective-c FSEventStreamCreate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆