Skip to content

Commit 1fe20c2

Browse files
author
Olivier Poitrey
committed
Remove the need for storeDataQueue dictionnary which required synchronization
1 parent 9f492cc commit 1fe20c2

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

SDImageCache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@interface SDImageCache : NSObject
1313
{
14-
NSMutableDictionary *memCache, *storeDataQueue;
14+
NSMutableDictionary *memCache;
1515
NSString *diskCachePath;
1616
NSOperationQueue *cacheInQueue, *cacheOutQueue;
1717
}

SDImageCache.m

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ - (id)init
2525
memCache = [[NSMutableDictionary alloc] init];
2626

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

@@ -75,7 +74,6 @@ - (void)dealloc
7574
[memCache release], memCache = nil;
7675
[diskCachePath release], diskCachePath = nil;
7776
[cacheInQueue release], cacheInQueue = nil;
78-
[storeDataQueue release], storeDataQueue = nil;
7977

8078
[[NSNotificationCenter defaultCenter] removeObserver:self];
8179

@@ -107,19 +105,17 @@ - (NSString *)cachePathForKey:(NSString *)key
107105
return [diskCachePath stringByAppendingPathComponent:filename];
108106
}
109107

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

115-
NSData *data = [storeDataQueue objectForKey:key];
113+
NSString *key = [keyAndData objectAtIndex:0];
114+
NSData *data = [keyAndData count] > 1 ? [keyAndData objectAtIndex:1] : nil;
115+
116116
if (data)
117117
{
118118
[fileManager createFileAtPath:[self cachePathForKey:key] contents:data attributes:nil];
119-
@synchronized(storeDataQueue)
120-
{
121-
[storeDataQueue removeObjectForKey:key];
122-
}
123119
}
124120
else
125121
{
@@ -193,8 +189,18 @@ - (void)storeImage:(UIImage *)image imageData:(NSData *)data forKey:(NSString *)
193189

194190
if (toDisk)
195191
{
196-
[storeDataQueue setObject:data forKey:key];
197-
[cacheInQueue addOperation:[[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(storeKeyToDisk:) object:key] autorelease]];
192+
NSArray *keyWithData;
193+
if (data)
194+
{
195+
keyWithData = [NSArray arrayWithObjects:key, data, nil];
196+
}
197+
else
198+
{
199+
keyWithData = [NSArray arrayWithObjects:key, nil];
200+
}
201+
[cacheInQueue addOperation:[[[NSInvocationOperation alloc] initWithTarget:self
202+
selector:@selector(storeKeyWithDataToDisk:)
203+
object:keyWithData] autorelease]];
198204
}
199205
}
200206

0 commit comments

Comments
 (0)