我已经成功地掌握了 FSEventStream 的基础知识,让我可以查看文件夹中的新文件事件。不幸的是,我传递给 FSEventStreamCreate() 的回调引用正在丢失/损坏/未保留,因此我也无法访问我需要的数据对象。以下是关键代码块:
FileWatcher.m:(设置 FSEvent 流)
FSEventStreamContext context;
//context.info = (__bridge_retained void *)(uploadQueue); // this didn't help
context.info = CFBridgingRetain(uploadQueue);
context.version = 0;
context.retain = NULL;
context.release = NULL;
context.copyDescription = NULL;
/* Create the stream, passing in a callback */
stream = FSEventStreamCreate(NULL,
&FileWatcherCallback,
&context,
pathsToWatch,
kFSEventStreamEventIdSinceNow, /* Or a previous event ID */
latency,
kFSEventStreamCreateFlagFileEvents /* Also add kFSEventStreamCreateFlagIgnoreSelf if lots of recursive callbacks */
);
Filewatcher.m:FileWatcherCallback
void FileWatcherCallback(
ConstFSEventStreamRef streamRef,
FSEventStreamContext *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[])
{
int i;
char **paths = eventPaths;
// Retrieve pointer to the download Queue!
NSMutableDictionary *queue = (NSMutableDictionary *)CFBridgingRelease(clientCallBackInfo->info);
// Try adding to the queue
[queue setValue:@"ToDownload" forKey:@"donkeytest" ];
...
}
当这个回调函数被触发时,我可以很好地获取文件路径,但是指向 NSMutableDictionary 的 clientCallBackInfo->info 指针现在指向的内存地址与我设置流时不同。然后当我尝试添加到字典时,我得到一个抛出异常(setValue 行)。
我是否需要以某种方式以不同的方式处理指针?任何帮助将非常感激。(我在 Xcode 4.5.1 上使用默认构建设置,包括 ARC。)