0

I need to get some files in definite folders. For this purpose I use this answer and it works fine. However, my folder structure is like:

Documents/
  idOfFolder1/
    images/
      image1.jpg
      image2.png
    sounds/
      sound1.mp3
    files/
      file1.pdf
  idOfFolder2/
    images/
      ...

I need to iterate through all folders having a specific id and get images. Since the answer written iterates through all folders it takes much more time than normal (we have many files). To optimize process, I want to skip "sounds" and "files" folders. Is there a way to handle this?

Thank you!

anyName
  • 113
  • 1
  • 2
  • 13

3 Answers3

1

If you don't like to use NSPredicate, following code will also do what you want,

(this code will directly access the folders with given ids and pick everything inside the images folders)

-(void) printImagesOfFolders:(NSArray *) folderIds{

    for(NSString *folderId in folderIds){

        NSArray *paths = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
        NSURL *documentsURL = [paths lastObject];
        NSString * imageFolderPath = [NSString stringWithFormat:@"%@/images", folderId];

        NSURL *rootURL = [documentsURL URLByAppendingPathComponent:imageFolderPath];
        NSFileManager *fm = [NSFileManager defaultManager];
        NSDirectoryEnumerator *dirEnumerator = [fm enumeratorAtURL:rootURL
                                        includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey]
                                                           options:NSDirectoryEnumerationSkipsHiddenFiles
                                                      errorHandler:nil];

        for (NSURL *url in dirEnumerator) {

            NSLog(@"url: %@", url);

        }

    }

}

How to use it

[self printImagesOfFolders:@[@"idOfFolder1",@"idOfFolder2",@"idOfFolder3",@"idOfFolder4"]];
Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45
0

I think you can implement it in a such different way using a NSPredicate onto the NSDirectoryEnumerator array as in this answer Iterating through files in nested folders or Filter NSFileManager with NSPredicate but the syntax can be awkward: take a look at NSPredicate syntax documentation NSPredicate Format String

In your case I would either: 1. use fast enumeration with a for loop 2. if possible redesign the folder structures 3. use core data or another caching system to speed up the loading process (this involves to sync time to time with the real files)

Lookaji
  • 1,023
  • 10
  • 21
  • I think making folder structure as not as folderId/images but images/folderId could be better as order and I can do this. What do you think? @lookaji – anyName Jun 05 '17 at 07:18
  • it does depend on what you are trying to achieve: if you structure as folder/images your searches will be indexed by folder, otherwise by images... If you want to have flexibility in your searches and lookups then I'd switch to CoreData. If your concern is speed, you'll have to write everything by yourself, of find a caching library. – Lookaji Jun 05 '17 at 07:44
0

That's right. The upstairs is right, and the index will be named directly based on the picture so that CPU can find the files accurately

刘明鑫
  • 111
  • 1
  • 4