I wrote a method(getDirTree1) that makes listing all directories from the root, using the recommended class NSDirectoryEnumerator and method nextObject. But while it was running unacceptably used a lot of memory (mainly private class NSPathStore2):
-(void) getDirTree1:(NSString*)directoryPath {
NSDirectoryEnumerator *dirEnum = [self->fileManager enumeratorAtPath:derectoryPath];
NSString *filePath;
NSString *fullFilePath;
while ( ( filePath = [ dirEnum nextObject ] ) != nil ) {
fullFilePath = [ directoryPath stringByAppendingPathComponent:filePath ];
NSLog( @"%@ \n", fullPath );
}
}
Assuming that this is because the object NSDirectoryEnumerator, I rewrote the method(getDirTree2). Now using the recursion, NSArray class and objectEnumerator method. But once again used a lot of memory.
-(void) getDirTree2:(NSString*)directoryPath {
NSArray *contents = [ self->fileManager contentsOfDirectoryAtPath:directoryPath error:NULL ];
NSEnumerator *enumeratorContent [ contents objectEnumerator ];
NSString *file;
BOOL fileIsDirectory = FALSE;
while ( ( file = [ enumeratorContent nextObject ] ) ) {
NSLog( @"%@ \n", [ directoryPath stringByAppendingPathComponent: file ] );
if ( [ self->fileManager fileExistAtPath:[ directoryPath stringByAppendingPathComponent:file ] isDirectory:&fileIsDirectory ] && fileIsDirectory )
[ self getDirTree2:[ directoryPath stringByAppendingPathComponent: file ] ];
}
}
What I missed(maybe I must dealloc/retain some objects) and how to do better. Thanks.