FSEvent - フォルダを監視する
そういえば以前、このアーティクルに目を通したおぼえがあって、へー、こんな便利な仕組みのあるのね、と考えたはず。
で、さらにFSEventでググってたどり着いたのがここ。
FSEvents Objective-C WrapperCベースのFSEventにObj-Cのラッパーをかぶせたクラスらしい。ありがたやー。
さっそくサンプルプロジェクトを開いてみる。
基本的にこんな感じでファイルシステムの監視ができるようだ。
- (void)setupEventListener
{
SCEvents *events = [SCEvents sharedPathWatcher];
[events setDelegate:self];
NSMutableArray *paths = [NSMutableArray arrayWithObject:NSHomeDirectory()];
NSMutableArray *excludePaths = [NSMutableArray arrayWithObject:[NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"]];
// Set the paths to be excluded
[events setExcludedPaths:excludePaths];
// Start receiving events
[events startWatchingPaths:paths];
// Display a description of the stream
NSLog(@"%@", [events streamDescription]);
}
/**
* This is the only method to be implemented to conform to the SCEventListenerProtocol.
* As this is only an example the event received is simply printed to the console.
*/
- (void)pathWatcher:(SCEvents *)pathWatcher eventOccurred:(SCEvent *)event
{
NSLog(@"%@", [event description]);
}
SCEventを扱う場合はNSMutableArray *paths = [NSMutableArray arrayWithObject:NSHomeDirectory()];
[events startWatchingPaths:paths];
基本、この2行でいいようだ。ふーむ、生のFSEvent使うより、素人にはこっちのほうがいいかも。
ただファイルマネージャなので頻繁に表示するディレクトリを切り替えることが想定される。ディレクトリが変わったら監視対象も変えないといけないが、そういう乱暴な用途に向くかどうか、明日以降やってみよう。
0 件のコメント:
コメントを投稿