Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect file change under document directory

My app has iTunes file sharing turned on, and when user add a file in iTunes, it should be able to detect.

The following function from Application Delegate is used to be called but not any more:

- (void)applicationDidBecomeActive:(UIApplication *)application

Is there an alternative solution?

like image 729
codercat Avatar asked Mar 21 '23 17:03

codercat


1 Answers

There is a solution: GCD. GCD allows you to create a dispatch_source_t that watches for vnode changes, like a write in a directory, and then notifies you about it. The basic is: Use the open(path, O_EVTONLY) syscall to obtain a file descriptor to the directory, then call dispatch_source_create()and pass DISPATCH_SOURCE_TYPE_VNODE as type and DISPATCH_VNODE_WRITE as mask. Set the event handler using dispatch_source_set_event_handler and do whatever you need to do when a file is written in the directory, additionally you also want to set a cancellation handler that calls close() to close the file descriptor. Finally, call dispatch_resume() to start monitoring.

like image 67
JustSid Avatar answered Apr 03 '23 03:04

JustSid