Skip to content

Commit 668341a

Browse files
fatalsunfacebook-github-bot
authored andcommitted
Ensure RCTImageCache's DateFormatter is only allocated once
Summary: This change attempts to fix a crash within RCTImageCache's new dateWithHeaderString method. This is a speculative fix as there aren't any concrete repro steps. Reviewed By: hramos Differential Revision: D13278666 fbshipit-source-id: cdb69b1296c946d89e14c074329280994d87ddcd
1 parent fe97458 commit 668341a

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

Libraries/Image/RCTImageCache.m

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ @implementation RCTImageCache
3232
NSOperationQueue *_imageDecodeQueue;
3333
NSCache *_decodedImageCache;
3434
NSMutableDictionary *_cacheStaleTimes;
35-
36-
NSDateFormatter *_headerDateFormatter;
3735
}
3836

3937
- (instancetype)init
@@ -50,7 +48,7 @@ - (instancetype)init
5048
selector:@selector(clearCache)
5149
name:UIApplicationWillResignActiveNotification
5250
object:nil];
53-
51+
5452
return self;
5553
}
5654

@@ -137,14 +135,16 @@ - (void)addImageToCache:(UIImage *)image
137135
}
138136

139137
- (NSDate *)dateWithHeaderString:(NSString *)headerDateString {
140-
if (_headerDateFormatter == nil) {
141-
_headerDateFormatter = [[NSDateFormatter alloc] init];
142-
_headerDateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
143-
_headerDateFormatter.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
144-
_headerDateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
145-
}
146-
147-
return [_headerDateFormatter dateFromString:headerDateString];
138+
static NSDateFormatter *formatter;
139+
static dispatch_once_t onceToken;
140+
dispatch_once(&onceToken, ^{
141+
formatter = [[NSDateFormatter alloc] init];
142+
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
143+
formatter.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
144+
formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
145+
});
146+
147+
return [formatter dateFromString:headerDateString];
148148
}
149149

150150
@end

0 commit comments

Comments
 (0)