0
我在我的XPC服務中使用FSEventStream有很大的麻煩(代碼如下)。服務啓動,流被創建,但回調函數永遠不會被調用。當我將完全相同的代碼複製到我的主應用程序並運行它時,它工作得很好。它可能是XPC服務無法使用的原因? 我試圖在這兩個部分禁用AppSandbox,但它沒有改變任何東西。任何幫助,這是高度讚賞。在XPC服務中使用FSEventStream不起作用
代碼:
- (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]);
}
}
感謝您的提示,這真是有趣的一點,我不知道。我會盡快回復它。順便說一句。 XPC沒有運行循環是怎麼回事?我認爲,當調用-resume方法時,它會創建它自己的運行循環,因爲它永遠不會返回。 – Matthes
另一個關於添加運行循環的SO問題:http://stackoverflow.com/questions/27806541/using-sockets-with-nsxpcconnection。要點在於:在你的XPC的info.plist中將'RunLoopType'設置爲'NSRunLoop'請參閱蘋果的XPC服務屬性列表鍵的文檔:https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup /Chapters/CreatingXPCServices.html –