user1467267
user1467267

Reputation:

attributesOfItemAtPath for many files

I have to match thousands of files from a webservice with the files on the disk of the iOS device. Currently I'm doing this in a loop and validating every file on disk with attributesOfItemAtPath on the shared NSFileManager to check if any files changed in size, but it feels wrong. Maybe looping over every file is the cleanest way, I was still wondering if there's a way to do attributesOfItemAtPath for every file in a directory (as NSArray or NSDictionary) upfront without harming excessive disk I/O.

The code runs in a loop and then for every file on disk it does:

fileAttributes = [_Core.fileMgr attributesOfItemAtPath:savePath error:nil];

if ([fileAttributes fileSize] != [[image valueForKey:@"filesize"] intValue]) {
    ... // the file has changed or is new
}

I'm already thinking of caching the last known file-sizes when it comes to re-loading the app through this loop, but I'm still wondering if there's a more efficient way to get attributes of many files in a more low-level efficient way.

Finally; this code is not giving me slowness. I'm merely wondering if there's a way to rule out excessive disk I/O, as sometimes this script downloads hundreds or thousands of MegaBytes of images and can take a long time and also does many disk-hits. Maybe there's something possible on a very low-level like NSPathUtilities? I'd rather cache 2~4MB of memory to cache a list then to hit the disk thousands of times without any added value.

Upvotes: 0

Views: 127

Answers (1)

Alistra
Alistra

Reputation: 5195

You still have to do the loop whenever you restart the app, but for handling changes as the app is running you can use something like https://github.com/bdkjones/VDKQueue or any other queue system for watching file system events.

I'm not sure what actually changes the files or do you have any way to have a hook, whenever something changes on the device, log the change and then just check against that change set with the server.

Upvotes: 1

Related Questions