-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DTFolderMonitor and integrated in DTZipArchive Demo
- Loading branch information
Showing
6 changed files
with
220 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// DTFolderMonitor.h | ||
// DTFoundation | ||
// | ||
// Created by Oliver Drobnik on 05.08.13. | ||
// Copyright (c) 2013 Cocoanetics. All rights reserved. | ||
// | ||
|
||
// The block to execute if a monitored folder changes | ||
typedef void (^DTFolderMonitorBlock) (void); | ||
|
||
/** | ||
Class for monitoring changes on a folder. This can be used to monitor the application documents folder for changes in the files there if the user adds or removes files via iTunes file sharing. | ||
*/ | ||
|
||
@interface DTFolderMonitor : NSObject | ||
|
||
/** | ||
@name Creating a Folder Monitor | ||
*/ | ||
|
||
/** | ||
Creates a new DTFolderMonitor to watch the folder at the given URL. Whenever there is a change on this folder the block is executed. | ||
The URL must be a file URL. Both the URL and the block parameter are mandatory. The block is being dispatched on a background queue. | ||
@param URL The monitored folder URL | ||
@param block The block to execute if the folder is being modified | ||
@returns The instantiated monitor in suspended mode. Call -startMonitoring to start monitoring. | ||
*/ | ||
+ (DTFolderMonitor *)folderMonitorForURL:(NSURL *)URL block:(DTFolderMonitorBlock)block; | ||
|
||
|
||
/** | ||
@name Starting/Stopping Monitoring | ||
*/ | ||
|
||
/** | ||
Start monitoring the folder. A monitor can be started and stopped multiple times. | ||
*/ | ||
- (void)startMonitoring; | ||
|
||
/** | ||
Stop monitoring the folder. A monitor can be started and stopped multiple times. | ||
*/ | ||
- (void)stopMonitoring; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// | ||
// DTFolderMonitor.m | ||
// DTFoundation | ||
// | ||
// Created by Oliver Drobnik on 05.08.13. | ||
// Copyright (c) 2013 Cocoanetics. All rights reserved. | ||
// | ||
|
||
#import "DTFolderMonitor.h" | ||
|
||
@implementation DTFolderMonitor | ||
{ | ||
NSURL *_URL; | ||
DTFolderMonitorBlock _block; | ||
|
||
int _fileDescriptor; | ||
dispatch_queue_t _queue; | ||
dispatch_source_t _source; | ||
} | ||
|
||
+ (DTFolderMonitor *)folderMonitorForURL:(NSURL *)URL block:(DTFolderMonitorBlock)block | ||
{ | ||
return [[DTFolderMonitor alloc] initWithURL:URL block:block]; | ||
} | ||
|
||
- (instancetype)initWithURL:(NSURL *)URL block:(DTFolderMonitorBlock)block | ||
{ | ||
NSParameterAssert(URL); | ||
NSParameterAssert(block); | ||
NSAssert([URL isFileURL], @"URL Parameter must be a folder URL"); | ||
|
||
self = [super init]; | ||
|
||
if (self) | ||
{ | ||
_URL = URL; | ||
_block = [block copy]; | ||
|
||
NSString *path = [_URL path]; | ||
_fileDescriptor = open([path fileSystemRepresentation], O_EVTONLY); | ||
|
||
if (!_fileDescriptor) | ||
{ | ||
return nil; | ||
} | ||
|
||
_queue = dispatch_queue_create("DTFolderMonitor Queue", 0); | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
[self stopMonitoring]; | ||
|
||
#if OS_OBJECT_USE_OBJC | ||
dispatch_release(_queue); | ||
#endif | ||
} | ||
|
||
- (void)startMonitoring | ||
{ | ||
@synchronized(self) | ||
{ | ||
if (_source) | ||
{ | ||
return; | ||
} | ||
|
||
// watch the file descriptor for writes | ||
_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, _fileDescriptor, DISPATCH_VNODE_WRITE, _queue); | ||
|
||
// call the passed block if the source is modified | ||
dispatch_source_set_event_handler(_source, _block); | ||
|
||
// close the file descriptor when the dispatch source is cancelled | ||
dispatch_source_set_cancel_handler(_source, ^{ | ||
|
||
close(_fileDescriptor); | ||
}); | ||
|
||
// at this point the dispatch source is paused, so start watching | ||
dispatch_resume(_source); | ||
} | ||
} | ||
|
||
- (void)stopMonitoring | ||
{ | ||
@synchronized(self) | ||
{ | ||
if (!_source) | ||
{ | ||
return; | ||
} | ||
|
||
dispatch_source_cancel(_source); | ||
|
||
#if !OS_OBJECT_USE_OBJC | ||
dispatch_release(_source); | ||
#endif | ||
_source = nil; | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters