DPlusV
DPlusV

Reputation: 14336

Automatically observing filesystem changes in UITableView

I'm hoping someone could suggest a way that I could automatically "observe" changes in a filesystem with a UITableView.

I have a UITableView populated with the contents of files in my directory.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager directoryContentsAtPath:documentsDirectory];

I then use this array in cellForRowAtIndexPath to display items. Now, if I add support for deleting items, there's an added step necessary: I have to both delete the physical file and update my array.

There's got to be a better way but I can't find it despite much searching.

Thanks!

Upvotes: 2

Views: 312

Answers (2)

Adrian Kosmaczewski
Adrian Kosmaczewski

Reputation: 7966

You could wrap NSFileManager in a class of your own, and this class would notify your code via KVO, NSNotification or an ad-hoc delegate. Such a class could be easily reused through different projects; here goes a fragment of the header of such a class:

@interface FileManagerWrapper : NSObject 
{
@private
    NSFileManager *_fileManager;
    NSString *_documentsDirectory;
    id<FileManagerWrapperDelegate> _delegate;

}

@property (nonatomic, copy) NSString *documentsDirectory;
@property (nonatomic, assign) id<FileManagerWrapperDelegate> delegate;

- (void)removeFile:(NSString *)path;

@end

Upvotes: 0

Felix
Felix

Reputation: 576

Usually this can be achieved by NSWorkspace... which is not supported on the iPhone. But since nobody except you should write in your documents directory you can easily implement a solution which includes this "added extra step". I do not think that this is too inelegant.

Upvotes: 1

Related Questions