我在XPC服务中使用FSEventStream遇到了大麻烦(下面的代码)。服务启动,流创建,但永远不会调用回调函数。当我将完全相同的代码复制到我的主应用程序并运行它时,它工作得很好。可能是因为它在XPC服务中不起作用的原因是什么? 我试图在这两个部分禁用AppSandbox,但它没有改变任何东西。对此有任何帮助表示高度赞赏。
代码:
- (void)initEventNotificationStreamForPath:(NSString *)path {
NPDLOG(@"Starting up FS event listener for path: %@", path);
NSArray *pathsToWatch = @[path];
FSEventStreamContext context;
context.info = (__bridge void *)self;
context.version = 0;
context.retain = NULL;
context.release = NULL;
context.copyDescription = NULL;
NSTimeInterval latency = 1.0;
_eventStream = FSEventStreamCreate(NULL, &eventNotificationCallback, &context, (__bridge CFArrayRef)pathsToWatch, kFSEventStreamEventIdSinceNow, //[lastEventID unsignedLongLongValue],
(CFAbsoluteTime)latency, (kFSEventStreamCreateFlagUseCFTypes | kFSEventStreamCreateFlagFileEvents | kFSEventStreamCreateFlagWatchRoot));
if(_eventStream) {
NPDLOG(@"Scheduling event stream on runloop");
FSEventStreamScheduleWithRunLoop(_eventStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
if(!FSEventStreamStart(_eventStream)) {
NPDLOG(@"Could NOT start event stream listener!!!");
}
else {
CFStringRef description = FSEventStreamCopyDescription(_eventStream);
NPDLOG(@"Stream description: %@", description);
CFRelease(description);
}
}
else {
NPDLOG(@"Could NOT create event stream listener!!!");
}
}
我的回调函数:
void eventNotificationCallback(ConstFSEventStreamRef streamRef, void *userData, size_t numEvents, void *eventPaths, const FSEventStreamEventFlags eventFlags[], const FSEventStreamEventId eventIds[]) {
//[((__bridge NPScannerServiceAgent *)userData).remoteObject didUpdateFilesAtPaths:(__bridge NSArray *)eventPaths];
printf("CALLBACK CALLED!!!\n");
NSLog(@"GOT FS CHANGE NOTIFICATION FROM %@", (__bridge NPScannerServiceAgent *)userData);
size_t i;
for(i = 0; i < numEvents; i++) {
NSLog(@"Modified path: %@, flags: %d", [(__bridge NSArray *)eventPaths objectAtIndex: i], eventFlags[i]);
}
}
答案 0 :(得分:1)
默认情况下,XPC服务没有运行循环。尝试使用FSEventStreamSetDispatchQueue()
而不是FSEventStreamScheduleWithRunLoop()
来在GCD队列而不是特定的运行循环上触发回调函数。