Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 388e926

Browse files
committed
feedback
1 parent a901576 commit 388e926

File tree

3 files changed

+61
-57
lines changed

3 files changed

+61
-57
lines changed

CodePush.m

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ @implementation CodePush {
2222
// These keys are already "namespaced" by the PendingUpdateKey, so
2323
// their values don't need to be obfuscated to prevent collision with app data
2424
static NSString *const PendingUpdateHashKey = @"hash";
25-
static NSString *const PendingUpdateWasInitializedKey = @"wasInitialized";
25+
static NSString *const PendingUpdateIsLoadingKey = @"isLoading";
2626

2727
@synthesize bridge = _bridge;
2828

@@ -114,19 +114,18 @@ - (void)initializeUpdateAfterRestart
114114
{
115115
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
116116
NSDictionary *pendingUpdate = [preferences objectForKey:PendingUpdateKey];
117-
118117
if (pendingUpdate) {
119118
_isFirstRunAfterUpdate = YES;
120-
BOOL wasInitialized = [pendingUpdate[PendingUpdateWasInitializedKey] boolValue];
121-
if (wasInitialized) {
119+
BOOL updateIsLoading = [pendingUpdate[PendingUpdateIsLoadingKey] boolValue];
120+
if (updateIsLoading) {
122121
// Pending update was initialized, but notifyApplicationReady was not called.
123122
// Therefore, deduce that it is a broken update and rollback.
124123
[self rollbackPackage];
125124
} else {
126125
// Mark that we tried to initialize the new update, so that if it crashes,
127126
// we will know that we need to rollback when the app next starts.
128127
[self savePendingUpdate:pendingUpdate[PendingUpdateHashKey]
129-
wasInitialized:YES];
128+
isLoading:YES];
130129
}
131130
}
132131
}
@@ -149,15 +148,19 @@ - (BOOL)isFailedHash:(NSString*)packageHash
149148
*/
150149
- (void)loadBundle
151150
{
152-
// If the current bundle URL is using http(s), then assume the dev
153-
// is debugging and therefore, shouldn't be redirected to a local
154-
// file (since Chrome wouldn't support it). Otherwise, update
155-
// the current bundle URL to point at the latest update
156-
if (![_bridge.bundleURL.scheme hasPrefix:@"http"]) {
157-
_bridge.bundleURL = [CodePush bundleURL];
158-
}
159-
160-
[_bridge reload];
151+
// This needs to be async dispatched because the _bridge is not set on init
152+
// when the app first starts, therefore rollbacks will not take effect.
153+
dispatch_async(dispatch_get_main_queue(), ^{
154+
// If the current bundle URL is using http(s), then assume the dev
155+
// is debugging and therefore, shouldn't be redirected to a local
156+
// file (since Chrome wouldn't support it). Otherwise, update
157+
// the current bundle URL to point at the latest update
158+
if (![_bridge.bundleURL.scheme hasPrefix:@"http"]) {
159+
_bridge.bundleURL = [CodePush bundleURL];
160+
}
161+
162+
[_bridge reload];
163+
});
161164
}
162165

163166
/*
@@ -178,6 +181,7 @@ - (void)rollbackPackage
178181
// Rollback to the previous version and de-register the new update
179182
[CodePushPackage rollbackPackage];
180183
[self removePendingUpdate];
184+
[self loadBundle];
181185
}
182186

183187
/*
@@ -203,8 +207,8 @@ - (void)saveFailedUpdate:(NSString *)packageHash
203207
}
204208

205209
/*
206-
* This method is called in notifyApplicationReady to register the fact that
207-
* the pending update succeeded and therefore can be removed.
210+
* This method is used to register the fact that a pending
211+
* update succeeded and therefore can be removed.
208212
*/
209213
- (void)removePendingUpdate
210214
{
@@ -219,14 +223,14 @@ - (void)removePendingUpdate
219223
* so that it can be used when the actual update application occurs at a later point.
220224
*/
221225
- (void)savePendingUpdate:(NSString *)packageHash
222-
wasInitialized:(BOOL)wasInitialized
226+
isLoading:(BOOL)isLoading
223227
{
224228
// Since we're not restarting, we need to store the fact that the update
225229
// was installed, but hasn't yet become "active".
226230
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
227231
NSDictionary *pendingUpdate = [[NSDictionary alloc] initWithObjectsAndKeys:
228232
packageHash,PendingUpdateHashKey,
229-
[NSNumber numberWithBool:wasInitialized],PendingUpdateWasInitializedKey, nil];
233+
[NSNumber numberWithBool:isLoading],PendingUpdateIsLoadingKey, nil];
230234

231235
[preferences setObject:pendingUpdate forKey:PendingUpdateKey];
232236
[preferences synchronize];
@@ -315,7 +319,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
315319
reject(error);
316320
} else {
317321
[self savePendingUpdate:updatePackage[@"packageHash"]
318-
wasInitialized:NO];
322+
isLoading:NO];
319323

320324
if (installMode == CodePushInstallModeImmediate) {
321325
[self loadBundle];

0 commit comments

Comments
 (0)