Is it possible to use the glob directory in iOS and get the results cached by the OS?

Many methods of reading from the file system using the NSFileManager and lower-level APIs in iOS involve built-in caching, so reading from directories that have not been changed can be quite fast, even if there are a lot of items in directories.

I have a situation where I want to be able to list files in a directory using glob:

i.e. the folder has files named like this:

 1-1-0.png 1-2-0.png 1-3-0.png 1-3-1.png 2-2-1.png 5-1-1.png 5-1-2.png 5-2-1.png 5-3-0.png 6-1-1.png ... 1501-5-2.png 

I would like to get all the file names corresponding to 5-*-1.png , which will return me 5-1-1.png and 5-2-1.png .

Downloading the full list of directories and then doing the substitution in RAM is quite simple, but is there any way to do this at the OS level, which will have built-in caching, so repeated calls to the same glob will give caching (faster) results?

+4
source share
1 answer

You can use the glob() function as indicated in this value: https://gist.github.com/bkyle/293959

 #include <glob.h> + (NSArray*) arrayWithFilesMatchingPattern: (NSString*) pattern inDirectory: (NSString*) directory { NSMutableArray* files = [NSMutableArray array]; glob_t gt; NSString* globPathComponent = [NSString stringWithFormat: @"/%@", pattern]; NSString* expandedDirectory = [directory stringByExpandingTildeInPath]; const char* fullPattern = [[expandedDirectory stringByAppendingPathComponent: globPathComponent] UTF8String]; if (glob(fullPattern, 0, NULL, &gt) == 0) { int i; for (i=0; i<gt.gl_matchc; i++) { size_t len = strlen(gt.gl_pathv[i]); NSString* filename = [[NSFileManager defaultManager] stringWithFileSystemRepresentation: gt.gl_pathv[i] length: len]; [files addObject: filename]; } } globfree(&gt); return [NSArray arrayWithArray: files]; } 
+1
source

Source: https://habr.com/ru/post/1439276/


All Articles