Permalink
Browse files

Initialize JMImageCacheDirectory and sharedCache atomically

  • Loading branch information...
1 parent a09d871 commit 0ad6fbd3d2315a2d0d14b8bf25d06dd1c17b7533 @jlnr jlnr committed Jan 7, 2013
Showing with 10 additions and 8 deletions.
  1. +10 −8 JMImageCache.m
View
@@ -8,12 +8,13 @@
#import "JMImageCache.h"
-static NSString *_JMImageCacheDirectory;
-
static inline NSString *JMImageCacheDirectory() {
- if(!_JMImageCacheDirectory) {
+ static NSString *_JMImageCacheDirectory;
+ static dispatch_once_t onceToken;
+
+ dispatch_once(&onceToken, ^{
_JMImageCacheDirectory = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/JMCache"] copy];
- }
+ });
return _JMImageCacheDirectory;
}
@@ -25,8 +26,6 @@
return [JMImageCacheDirectory() stringByAppendingPathComponent:fileName];
}
-JMImageCache *_sharedCache = nil;
-
@interface JMImageCache ()
@property (strong, nonatomic) NSOperationQueue *diskOperationQueue;
@@ -40,9 +39,12 @@ @implementation JMImageCache
@synthesize diskOperationQueue = _diskOperationQueue;
+ (JMImageCache *) sharedCache {
- if(!_sharedCache) {
+ static JMImageCache *_sharedCache = nil;
+ static dispatch_once_t onceToken;
+
+ dispatch_once(&onceToken, ^{
_sharedCache = [[JMImageCache alloc] init];
- }
+ });
return _sharedCache;
}

0 comments on commit 0ad6fbd

Please sign in to comment.