Database Reference
In-Depth Information
if
(!path)
return
NO;
BOOL directory = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
if
(![fileManager fileExistsAtPath:path isDirectory:&directory]) {
if
(![fileManager createDirectoryAtPath:path
withIntermediateDirectories:YES
attributes:nil
error:error]) {
return
NO;
}
directory = YES;
}
if
(!directory) {
NSMutableDictionary *errorDict = [NSMutableDictionary dictionary];
NSString *msg = NSLocalizedString(@
"File in place of metadata directory"
,
@
"metadata directory is a file error description"
);
[errorDict setValue:msg forKey:NSLocalizedDescriptionKey];
*error = [NSError errorWithDomain:@
"pragprog"
code:1001 userInfo:errorDict];
return
NO;
}
Because we want to be in the habit of assuming nothing, let's first check that
there is something to update or delete. Once we are past that check, we next
need to confirm that the cache directory is in place, and either our metadata
directory is in place or we can create it. If any of this fails, we update the
NSError
object and return.
NSString *filePath = nil;
if
(deletedObjects && [deletedObjects count]) {
for
(NSString *filename in deletedObjects) {
filePath = [path stringByAppendingPathComponent:filename];
if
(![fileManager fileExistsAtPath:filePath])
continue
;
if
(![fileManager removeItemAtPath:filePath error:error])
return
NO;
}
}
The next part of updating the metadata is to remove any files that are no
longer appropriate. Therefore, if the passed-in
deletedObjects
set contains any
objects, we need to loop over it. Since we know that the name of the metadata
file is stored in the
deletedObjects
variable, we append it to the metadata direc-
tory path and check for the existence of the file. If it exists, we delete it. (It
may be possible that a recipe got created and deleted without ever being saved
to disk. It's unlikely, but why take chances?) If we run into an issue deleting
the file, we abort the update and let the calling method handle the error.