Observe a File or Folder in Objective-C
What is the best way to listen to a folder or file to see if it has been saved or if a new file has been added?
- When should I remove observers? Error about deallocating objects before removing observers
- Observing change in frame of a UIView during animation
- ios observing change in frame of a UIView during animation
- Observing Asynchronous Requests
- Observer Pattern in Swift
- How do you determine if an object (self) with a lot of properties has been changed?
5 Solutions Collect From Internet About “Observe a File or Folder in Objective-C”
The FSEvents API is ideal if you just want to watch directories but it doesn’t handle the monitoring of individual files. Stu Connolly has a great Objective-C wrapper for the FSEvents C API, it’s called SCEvents and you can get it here:
The nice thing about FSEvents is that you just need to watch one folder and you will be notified of any changes that occur anywhere in the subfolder hierarchy of that folder.
If you need file-level notifications you will need to use kqueues. Uli Kusterer has a great Objective-C wrapper:
Either of these methods is a lot easier than wrangling with the C APIs directly, which are not particularly well documented and a bit obtuse.
If you need to support Tiger you’ll need to use kqueues as the FSEvents API wasn’t officially available in 10.4.
Try using FSEvents, although it is a C API
OS 10.5 or newer
If you are changing a file or folder, I believe the Spotlight search engine will update its database to reflect your changes.
So you might set up a thread that listens for kMDQueryDidUpdateNotification notifications through a Spotlight query specific to that file or folder.
When you get those notifications, you could fire a selector that does something you want.
If you do need to use kqueue (as discussed in other answers) Google Toolbox for Mac has a nice Objective-C wrapper that I’ve used with no issues thus far.
Not sure what’s the best way, but A way would be to fire up an NSThread that would regularly (for instance every second) check the creation dates of the files in the directory, and then have a delegate associated with that thread to perform some action when a new file has been added
- Animate UIButton state change
- Xcode Scene Dock hidden
- Can I bind different associated values in a Swift enum to the same var?
- How can I generate UML diagrams from Xcode4
- Getting “Cannot verify client. (3000)” when trying to upload archive from Xcode to itunesConnect
- NSDictionary with ordered keys
- How to #include hash with ext, tr1, or __gnu_cxx in XCode, C++
- Exclude from iCloud backup: NSURLIsExcludedFromBackupKey
- Getting metadata from an audio stream
- Xcode Project vs. Xcode Workspace – Differences
- Sound when the app is in the Background iOS
- Swift 3 Alamofire multipart upload
- Determine if a video file is NDF or DF (Drop Frame or Non-Drop Frame)
- Set the title of Navigation Bar created in Interface Builder
- React Native using pre-bundled file on device, even if DEV-mode