Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

cocoa - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

http://stuconnolly.com/blog/scevents-011/

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:

http://zathras.de/angelweb/sourcecode.htm#UKKQueue

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...