我创建了一个FSEvent来处理文件更改。我的问题是,我第一次保存文件(它发生在Microsoft文件,例如“.docx”,“。xls”...文件),事件被触发两次。我需要显示用户消息,并显示两次。这是我的源代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *application_directory = [paths objectAtIndex:0];
NSString *folder_path = [NSString stringWithFormat:@"%@/%@", application_directory, path];
CFStringRef file_path_ref = (CFStringRef) folder_path;
CFArrayRef pathsToWatch = CFArrayCreate(NULL, (const void **)&file_path_ref, 1, NULL);
void *appPointer = (void *)self;
FSEventStreamContext context = {0, appPointer, NULL, NULL, NULL}; // could put stream-specific data here.
CFAbsoluteTime latency = 3.0; /* Latency in seconds */
//Create the stream, passing in a callback
stream = FSEventStreamCreate(NULL,
&mycallback,
&context,
pathsToWatch,
kFSEventStreamEventIdSinceNow,
latency,
kFSEventStreamCreateFlagUseCFTypes
);
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
CFRunLoopRun();
这是回调方法:
void mycallback(ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]){
int i;
for (i=0; i<numEvents; i++)
{
DirectoryWatcher *directory_watcher = (DirectoryWatcher *)clientCallBackInfo;
NSFileManager *file_manager = [NSFileManager defaultManager];
Common *common = [[Common alloc] init];
NSString *file_path = [common filePathByNameAndId:directory_watcher.file_name file_id:directory_watcher.file_id];
NSDictionary *fileAttributes = [file_manager attributesOfItemAtPath:file_path error:NULL];
[common release];
NSDate *modified_date = [fileAttributes objectForKey:@"NSFileModificationDate"];
NSDate *saved_date = [[NSUserDefaults standardUserDefaults] objectForKey:@"kFeedLastModified"];
[[NSUserDefaults standardUserDefaults] setObject:modified_date forKey:@"kFeedLastModified"];
[[NSUserDefaults standardUserDefaults] synchronize];
if (![modified_date isEqualToDate: saved_date])
{
Common *common = [[Common alloc] init];
if([common networkConectivityAvailable])
{
//HERE IS MY USER MESSAGE
}
else
{
NSLog(@"Retry this flow when internet is restored.");
}
[common release];
}
} }
答案 0 :(得分:0)
听起来微软在写出文件时做了一些愚蠢的事情......实际上,你永远不会指望每个应用程序的行为一致和/或正确。
我的建议是只显示一个警报(如果还没有显示)(即可以解决从Microsoft应用程序触发的多个FSEvents的问题)。或者将警报排队并按顺序显示它们,确保仅在适当时显示后续警报(即,当已经过了一定的时间或后续操作是在不同的文件上时等)。