Skip to content

Commit

Permalink
Only create the directory if we failed to write a file
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbrown committed Aug 27, 2012
1 parent 860b821 commit 1652105
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
15 changes: 14 additions & 1 deletion SeismicJSON/ImageFetchOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,23 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (self.response.statusCode==200) {
NSError *error=nil;
NSString *filename = [self.urlToFetch lastPathComponent];
NSString *fullFilePath = [[[NetworkManager sharedManager] cachedImageDirectory] stringByAppendingPathComponent:filename];
NSString *cachedImageDirectory = [[NetworkManager sharedManager] cachedImageDirectory];
NSString *fullFilePath = [cachedImageDirectory stringByAppendingPathComponent:filename];
NSLog(@"About to write file: %@",fullFilePath);
if (![self.fetchedData writeToFile:fullFilePath options:NSDataWritingAtomic error:&error]) {
NSLog(@"error occurred writing file: %@",[error localizedDescription]);
BOOL isDirectory=NO;
if (![[NSFileManager defaultManager] fileExistsAtPath:cachedImageDirectory isDirectory:&isDirectory]) {
NSError *error=nil;
if (![[NSFileManager defaultManager] createDirectoryAtPath:cachedImageDirectory withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"Error creating directory '%@':%@",cachedImageDirectory,[error localizedDescription]);
} else {
if (![self.fetchedData writeToFile:fullFilePath options:NSDataWritingAtomic error:&error]) {
NSLog(@"error occurred writing file again: %@",[error localizedDescription]);
}
}
}

}
if (self.notificationTarget) {
[self.notificationTarget imageDidBecomeAvailableAtPath:fullFilePath];
Expand Down
7 changes: 0 additions & 7 deletions SeismicJSON/NetworkManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,6 @@ -(void) decrementActiveFetches {
-(NSString *) cachedImageDirectory {
if (_cachedImageDirectory==nil) {
_cachedImageDirectory = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"cachedImages"];
BOOL isDirectory=NO;
if (![[NSFileManager defaultManager] fileExistsAtPath:_cachedImageDirectory isDirectory:&isDirectory]) {
NSError *error=nil;
if (![[NSFileManager defaultManager] createDirectoryAtPath:_cachedImageDirectory withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"Error creating directory '%@':%@",_cachedImageDirectory,[error localizedDescription]);
}
}
}

return _cachedImageDirectory;
Expand Down

0 comments on commit 1652105

Please sign in to comment.