From 319fa03125532c8e9ff2ee9ccddfd744d5b6bd05 Mon Sep 17 00:00:00 2001 From: Albert Sun Date: Fri, 30 Nov 2018 15:02:49 -0800 Subject: [PATCH] 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 --- Libraries/Image/RCTImageCache.m | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Libraries/Image/RCTImageCache.m b/Libraries/Image/RCTImageCache.m index 67848f9c4c8..ca116b5d03a 100644 --- a/Libraries/Image/RCTImageCache.m +++ b/Libraries/Image/RCTImageCache.m @@ -32,8 +32,6 @@ static NSString *RCTCacheKeyForImage(NSString *imageTag, CGSize size, CGFloat sc NSOperationQueue *_imageDecodeQueue; NSCache *_decodedImageCache; NSMutableDictionary *_cacheStaleTimes; - - NSDateFormatter *_headerDateFormatter; } - (instancetype)init @@ -50,7 +48,7 @@ static NSString *RCTCacheKeyForImage(NSString *imageTag, CGSize size, CGFloat sc selector:@selector(clearCache) name:UIApplicationWillResignActiveNotification object:nil]; - + return self; } @@ -137,14 +135,16 @@ static NSString *RCTCacheKeyForImage(NSString *imageTag, CGSize size, CGFloat sc } - (NSDate *)dateWithHeaderString:(NSString *)headerDateString { - if (_headerDateFormatter == nil) { - _headerDateFormatter = [[NSDateFormatter alloc] init]; - _headerDateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; - _headerDateFormatter.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'"; - _headerDateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; - } + static NSDateFormatter *formatter; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + formatter = [[NSDateFormatter alloc] init]; + formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; + formatter.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'"; + formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; + }); - return [_headerDateFormatter dateFromString:headerDateString]; + return [formatter dateFromString:headerDateString]; } @end