From 3e4ac610fe2a9826756005f0ee0bf44266c56d70 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Fri, 22 Jan 2016 01:44:38 -0800 Subject: [PATCH] fix rollback bug --- CodePush.js | 2 +- CodePush.m | 10 +++++----- Examples/CodePushDemoApp/crossplatformdemo.js | 4 ++++ .../java/com/microsoft/codepush/react/CodePush.java | 8 ++++---- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/CodePush.js b/CodePush.js index f295c5a..993a412 100644 --- a/CodePush.js +++ b/CodePush.js @@ -115,7 +115,7 @@ function getPromisifiedSdk(requestFetchAdapter, config) { }); }; - sdk.reportStatusDownload = (downloadedPackage, status) => { + sdk.reportStatusDownload = (downloadedPackage) => { return new Promise((resolve, reject) => { module.exports.AcquisitionSdk.prototype.reportStatusDownload.call(sdk, downloadedPackage, (err) => { if (err) { diff --git a/CodePush.m b/CodePush.m index f431550..3b69041 100644 --- a/CodePush.m +++ b/CodePush.m @@ -13,7 +13,7 @@ RCT_EXPORT_MODULE() -static BOOL didRollback = NO; +static BOOL needToReportRollback = NO; static BOOL isRunningBinaryVersion = NO; static BOOL testConfigurationFlag = NO; @@ -195,15 +195,14 @@ static NSString *const PackageIsPendingKey = @"isPending"; NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults]; NSDictionary *pendingUpdate = [preferences objectForKey:PendingUpdateKey]; if (pendingUpdate) { - didRollback = NO; _isFirstRunAfterUpdate = YES; BOOL updateIsLoading = [pendingUpdate[PendingUpdateIsLoadingKey] boolValue]; if (updateIsLoading) { // Pending update was initialized, but notifyApplicationReady was not called. // Therefore, deduce that it is a broken update and rollback. NSLog(@"Update did not finish loading the last time, rolling back to a previous version."); + needToReportRollback = YES; [self rollbackPackage]; - didRollback = YES; } else { // Mark that we tried to initialize the new update, so that if it crashes, // we will know that we need to rollback when the app next starts. @@ -293,7 +292,7 @@ static NSString *const PackageIsPendingKey = @"isPending"; - (void)recordDeploymentStatusReported:(NSString *)appVersionOrPackageIdentifier { NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults]; - [preferences setValue:LastDeploymentReportKey forKey:appVersionOrPackageIdentifier]; + [preferences setValue:appVersionOrPackageIdentifier forKey:LastDeploymentReportKey]; [preferences synchronize]; } @@ -534,8 +533,9 @@ RCT_EXPORT_METHOD(notifyApplicationReady:(RCTPromiseResolveBlock)resolve RCT_EXPORT_METHOD(getNewStatusReport:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - if (didRollback) { + if (needToReportRollback) { // Check if there was a rollback that was not yet reported + needToReportRollback = NO; NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults]; NSMutableArray *failedUpdates = [preferences objectForKey:FailedUpdatesKey]; if (failedUpdates) { diff --git a/Examples/CodePushDemoApp/crossplatformdemo.js b/Examples/CodePushDemoApp/crossplatformdemo.js index b340b3f..d941af0 100644 --- a/Examples/CodePushDemoApp/crossplatformdemo.js +++ b/Examples/CodePushDemoApp/crossplatformdemo.js @@ -81,6 +81,10 @@ let CodePushDemoApp = React.createClass({ } }, + componentDidMount() { + CodePush.notifyApplicationReady(); + }, + getInitialState() { return { }; }, diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java index 4474074..8d7d64a 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java @@ -40,7 +40,7 @@ import java.util.zip.ZipFile; public class CodePush { - private static boolean didRollback = false; + private static boolean needToReportRollback = false; private static boolean isRunningBinaryVersion = false; private static boolean testConfigurationFlag = false; @@ -237,15 +237,14 @@ public class CodePush { JSONObject pendingUpdate = getPendingUpdate(); if (pendingUpdate != null) { didUpdate = true; - didRollback = false; try { boolean updateIsLoading = pendingUpdate.getBoolean(PENDING_UPDATE_IS_LOADING_KEY); if (updateIsLoading) { // Pending update was initialized, but notifyApplicationReady was not called. // Therefore, deduce that it is a broken update and rollback. CodePushUtils.log("Update did not finish loading the last time, rolling back to a previous version."); + needToReportRollback = true; rollbackPackage(); - didRollback = true; } else { // Clear the React dev bundle cache so that new updates can be loaded. if (this.isDebugMode) { @@ -457,8 +456,9 @@ public class CodePush { @ReactMethod public void getNewStatusReport(Promise promise) { - if (didRollback) { + if (needToReportRollback) { // Check if there was a rollback that was not yet reported + needToReportRollback = false; JSONArray failedUpdates = getFailedUpdates(); if (failedUpdates != null && failedUpdates.length() > 0) { try {