Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve perf 3-5x by avoiding re-instantiating date formatter #356

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions RCTAppleHealthKit/RCTAppleHealthKit+Utils.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,31 @@ + (NSArray *)formatWorkoutEvents:(NSArray *)workoutEvents
return formattedWorkEvents;
}

+ (NSDate *)parseISO8601DateFromString:(NSString *)date
{
@try {
NSDateFormatter *dateFormatter = [NSDateFormatter new];

+ (NSDateFormatter *)iso8601DateFormatter {
static NSDateFormatter *dateFormatter = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dateFormatter = [NSDateFormatter new];
NSLocale *posix = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
dateFormatter.locale = posix;
dateFormatter.dateFormat = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZ";
return [dateFormatter dateFromString:date];
});
return dateFormatter;
}

+ (NSDate *)parseISO8601DateFromString:(NSString *)date {
@try {
return [[RCTAppleHealthKit iso8601DateFormatter] dateFromString:date];
} @catch (NSException *exception) {
NSLog(@"RNHealth: An error occured while trying parse ISO8601 date from string");
return nil;
}
}


+ (NSString *)buildISO8601StringFromDate:(NSDate *)date
{
+ (NSString *)buildISO8601StringFromDate:(NSDate *)date {
@try {
NSDateFormatter *dateFormatter = [NSDateFormatter new];
NSLocale *posix = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
dateFormatter.locale = posix;
dateFormatter.dateFormat = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZ";
return [dateFormatter stringFromDate:date];
return [[RCTAppleHealthKit iso8601DateFormatter] stringFromDate:date];
} @catch (NSException *exception) {
NSLog(@"RNHealth: An error occured while trying parse ISO8601 string from date");
return nil;
Expand Down