Skip to content

Commit

Permalink
Remove the need for storeDataQueue dictionnary which required synchro…
Browse files Browse the repository at this point in the history
…nization
  • Loading branch information
Olivier Poitrey committed Oct 9, 2010
1 parent 9f492cc commit 1fe20c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion SDImageCache.h
Expand Up @@ -11,7 +11,7 @@

@interface SDImageCache : NSObject
{
NSMutableDictionary *memCache, *storeDataQueue;
NSMutableDictionary *memCache;
NSString *diskCachePath;
NSOperationQueue *cacheInQueue, *cacheOutQueue;
}
Expand Down
26 changes: 16 additions & 10 deletions SDImageCache.m
Expand Up @@ -25,7 +25,6 @@ - (id)init
memCache = [[NSMutableDictionary alloc] init];

// Init the disk cache
storeDataQueue = [[NSMutableDictionary alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
diskCachePath = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"ImageCache"] retain];

Expand Down Expand Up @@ -75,7 +74,6 @@ - (void)dealloc
[memCache release], memCache = nil;
[diskCachePath release], diskCachePath = nil;
[cacheInQueue release], cacheInQueue = nil;
[storeDataQueue release], storeDataQueue = nil;

[[NSNotificationCenter defaultCenter] removeObserver:self];

Expand Down Expand Up @@ -107,19 +105,17 @@ - (NSString *)cachePathForKey:(NSString *)key
return [diskCachePath stringByAppendingPathComponent:filename];
}

- (void)storeKeyToDisk:(NSString *)key
- (void)storeKeyWithDataToDisk:(NSArray *)keyAndData
{
// Can't use defaultManager another thread
NSFileManager *fileManager = [[NSFileManager alloc] init];

NSData *data = [storeDataQueue objectForKey:key];
NSString *key = [keyAndData objectAtIndex:0];
NSData *data = [keyAndData count] > 1 ? [keyAndData objectAtIndex:1] : nil;

if (data)
{
[fileManager createFileAtPath:[self cachePathForKey:key] contents:data attributes:nil];
@synchronized(storeDataQueue)
{
[storeDataQueue removeObjectForKey:key];
}
}
else
{
Expand Down Expand Up @@ -193,8 +189,18 @@ - (void)storeImage:(UIImage *)image imageData:(NSData *)data forKey:(NSString *)

if (toDisk)
{
[storeDataQueue setObject:data forKey:key];
[cacheInQueue addOperation:[[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(storeKeyToDisk:) object:key] autorelease]];
NSArray *keyWithData;
if (data)
{
keyWithData = [NSArray arrayWithObjects:key, data, nil];
}
else
{
keyWithData = [NSArray arrayWithObjects:key, nil];
}
[cacheInQueue addOperation:[[[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(storeKeyWithDataToDisk:)
object:keyWithData] autorelease]];
}
}

Expand Down

0 comments on commit 1fe20c2

Please sign in to comment.