0
ファイルの変更を処理するFSEventを作成しました。私の問題は、ファイルを初めて保存するときです(Microsoftのファイル、たとえば ".docx"、 ".xls" ...ファイルなど)。イベントは2回発生します。私はユーザーメッセージを表示する必要があり、それは2回表示されます。ここに私のソースコードは次のとおりです。ファイルが保存されるときにFSEventが2回発砲しています
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();
そして、これは、コールバックメソッドです:ファイルを書き出すときに、Microsoftは、いくつかの間抜けなものをやっている...そして実際に、あなたが数えることはできませんように
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];
}
} }