From d114b90f649288e34b0b5d83e3b7f3d053adca9d Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Fri, 5 Feb 2016 02:51:12 -0800 Subject: [PATCH] use string comparison for date --- CodePush.m | 24 ++++++++++++++++++------ CodePushPackage.m | 5 +++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CodePush.m b/CodePush.m index 317e310..49e64a7 100644 --- a/CodePush.m +++ b/CodePush.m @@ -24,6 +24,7 @@ static NSString *const DeploymentFailed = @"DeploymentFailed"; static NSString *const DeploymentSucceeded = @"DeploymentSucceeded"; // These keys represent the names we use to store data in NSUserDefaults +static NSString *const BinaryBundleDateKey = @"CODE_PUSH_BINARY_DATE"; static NSString *const FailedUpdatesKey = @"CODE_PUSH_FAILED_UPDATES"; static NSString *const PendingUpdateKey = @"CODE_PUSH_PENDING_UPDATE"; @@ -56,6 +57,11 @@ static NSString *const PackageIsPendingKey = @"isPending"; NSError *error; NSString *packageFile = [CodePushPackage getCurrentPackageBundlePath:&error]; NSURL *binaryJsBundleUrl = [[NSBundle mainBundle] URLForResource:resourceName withExtension:resourceExtension]; + NSDictionary *binaryFileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[binaryJsBundleUrl path] error:nil]; + NSDate *binaryDate = [binaryFileAttributes objectForKey:NSFileModificationDate]; + NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults]; + [preferences setObject:binaryDate forKey:FailedUpdatesKey]; + [preferences synchronize]; NSString *logMessageFormat = @"Loading JS bundle from %@"; @@ -65,10 +71,6 @@ static NSString *const PackageIsPendingKey = @"isPending"; return binaryJsBundleUrl; } - NSDictionary *binaryFileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[binaryJsBundleUrl path] error:nil]; - NSDictionary *appFileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:packageFile error:nil]; - NSDate *binaryDate = [binaryFileAttributes objectForKey:NSFileModificationDate]; - NSDate *packageDate = [appFileAttribs objectForKey:NSFileModificationDate]; NSString *binaryAppVersion = [[CodePushConfig current] appVersion]; NSDictionary *currentPackageMetadata = [CodePushPackage getCurrentPackage:&error]; if (error || !currentPackageMetadata) { @@ -77,9 +79,11 @@ static NSString *const PackageIsPendingKey = @"isPending"; return binaryJsBundleUrl; } + NSString *packageDate = [currentPackageMetadata objectForKey:BinaryBundleDateKey]; + NSString *binaryDateString = [NSString stringWithFormat:@"%f", [binaryDate timeIntervalSince1970]]; NSString *packageAppVersion = [currentPackageMetadata objectForKey:@"appVersion"]; - if ([binaryDate compare:packageDate] == NSOrderedAscending && ([CodePush isUsingTestConfiguration] ||[binaryAppVersion isEqualToString:packageAppVersion])) { + if ([binaryDateString isEqualToString:packageDate] && ([CodePush isUsingTestConfiguration] ||[binaryAppVersion isEqualToString:packageAppVersion])) { // Return package file because it is newer than the app store binary's JS bundle NSURL *packageUrl = [[NSURL alloc] initFileURLWithPath:packageFile]; NSLog(logMessageFormat, packageUrl); @@ -362,11 +366,19 @@ static NSString *const PackageIsPendingKey = @"isPending"; /* * This is native-side of the RemotePackage.download method */ -RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary*)updatePackage +RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary*)immutableUpdatePackage resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { dispatch_async(dispatch_get_main_queue(), ^{ + NSDictionary* updatePackage = [immutableUpdatePackage mutableCopy]; + NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults]; + NSDate *binaryBundleDate = [preferences objectForKey:BinaryBundleDateKey]; + if (binaryBundleDate != nil) { + [updatePackage setValue:[NSString stringWithFormat:@"%f", [binaryBundleDate timeIntervalSince1970]] + forKey:BinaryBundleDateKey]; + } + [CodePushPackage downloadPackage:updatePackage // The download is progressing forward progressCallback:^(long long expectedContentLength, long long receivedContentLength) { diff --git a/CodePushPackage.m b/CodePushPackage.m index 888c080..10a6c78 100644 --- a/CodePushPackage.m +++ b/CodePushPackage.m @@ -285,6 +285,11 @@ NSString * const UnzippedFolderName = @"unzipped"; [CodePushPackage copyEntriesInFolder:unzippedFolderPath destFolder:newPackageFolderPath error:&error]; + if (error) { + failCallback(error); + return; + } + [[NSFileManager defaultManager] removeItemAtPath:unzippedFolderPath error:&nonFailingError]; if (nonFailingError) {