Storing paths
In HouseCat I store the full path to the image files (full-size and thumbnails) in a CoreData entity. An example would be:
/var/mobile/Containers/Data/Application/7DEFAD28-E1B8-4851-996C-355A6ED0C14F/Documents/img/4FBC04DA-AA9C-46DE-A4EF-B51EABF0F40B.png
That path is built via:
// all images (thumbs & full-size) will be stored in this dir:
NSString *imgDir = @”/img/”;
CFUUIDRef theUUID = CFUUIDCreate(kCFAllocatorDefault);
NSString *imgName = CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, theUUID));
CFRelease(theUUID);
NSString *imgSuffix =@”.png”;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@”%@%@%@%@”, documentsDir, imgDir, imgName, imgSuffix];
I recently ran into a problem where the path became invalid resulting in, among other problems, empty grey boxes where there should be an image. Not good.
Oh snap.
The solution is simple, in the CoreData entity just store the sub-dir under the application dir and then dynamically prepend the current app directory at run-time.
Now I need to figure out how to fix this for all existing customers that upgrade to the new version. I’m mulling several options but I think the optimal solution will be to check some user prefs flag on startup and if it indicates that the db is an older version then I’ll just iterate over all the records and fix them with a regex.
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDir = [paths objectAtIndex:0];