Permalink
Browse files

Merge pull request #18 from jlnr/master

Initialize static variables atomically
  • Loading branch information...
jakemarsh committed Mar 10, 2013
2 parents 4745f3d + 0ad6fbd commit 36c6c697b170fa5f6075fa25cd47648ea2685fef
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 36c6c69

Please sign in to comment.