Skip to content

Commit

Permalink
Ensure RCTImageCache's DateFormatter is only allocated once
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fatalsun authored and facebook-github-bot committed Nov 30, 2018
1 parent fe97458 commit 668341a
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions Libraries/Image/RCTImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ @implementation RCTImageCache
NSOperationQueue *_imageDecodeQueue;
NSCache *_decodedImageCache;
NSMutableDictionary *_cacheStaleTimes;

NSDateFormatter *_headerDateFormatter;
}

- (instancetype)init
Expand All @@ -50,7 +48,7 @@ - (instancetype)init
selector:@selector(clearCache)
name:UIApplicationWillResignActiveNotification
object:nil];

return self;
}

Expand Down Expand Up @@ -137,14 +135,16 @@ - (void)addImageToCache:(UIImage *)image
}

- (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];
}

return [_headerDateFormatter dateFromString:headerDateString];
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 [formatter dateFromString:headerDateString];
}

@end

0 comments on commit 668341a

Please sign in to comment.