@@ -22,7 +22,7 @@ @implementation CodePush {
22
22
// These keys are already "namespaced" by the PendingUpdateKey, so
23
23
// their values don't need to be obfuscated to prevent collision with app data
24
24
static NSString *const PendingUpdateHashKey = @" hash" ;
25
- static NSString *const PendingUpdateWasInitializedKey = @" wasInitialized " ;
25
+ static NSString *const PendingUpdateIsLoadingKey = @" isLoading " ;
26
26
27
27
@synthesize bridge = _bridge;
28
28
@@ -114,19 +114,18 @@ - (void)initializeUpdateAfterRestart
114
114
{
115
115
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults ];
116
116
NSDictionary *pendingUpdate = [preferences objectForKey: PendingUpdateKey];
117
-
118
117
if (pendingUpdate) {
119
118
_isFirstRunAfterUpdate = YES ;
120
- BOOL wasInitialized = [pendingUpdate[PendingUpdateWasInitializedKey ] boolValue ];
121
- if (wasInitialized ) {
119
+ BOOL updateIsLoading = [pendingUpdate[PendingUpdateIsLoadingKey ] boolValue ];
120
+ if (updateIsLoading ) {
122
121
// Pending update was initialized, but notifyApplicationReady was not called.
123
122
// Therefore, deduce that it is a broken update and rollback.
124
123
[self rollbackPackage ];
125
124
} else {
126
125
// Mark that we tried to initialize the new update, so that if it crashes,
127
126
// we will know that we need to rollback when the app next starts.
128
127
[self savePendingUpdate: pendingUpdate[PendingUpdateHashKey]
129
- wasInitialized :YES ];
128
+ isLoading :YES ];
130
129
}
131
130
}
132
131
}
@@ -149,15 +148,19 @@ - (BOOL)isFailedHash:(NSString*)packageHash
149
148
*/
150
149
- (void )loadBundle
151
150
{
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
+ });
161
164
}
162
165
163
166
/*
@@ -178,6 +181,7 @@ - (void)rollbackPackage
178
181
// Rollback to the previous version and de-register the new update
179
182
[CodePushPackage rollbackPackage ];
180
183
[self removePendingUpdate ];
184
+ [self loadBundle ];
181
185
}
182
186
183
187
/*
@@ -203,8 +207,8 @@ - (void)saveFailedUpdate:(NSString *)packageHash
203
207
}
204
208
205
209
/*
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.
208
212
*/
209
213
- (void )removePendingUpdate
210
214
{
@@ -219,14 +223,14 @@ - (void)removePendingUpdate
219
223
* so that it can be used when the actual update application occurs at a later point.
220
224
*/
221
225
- (void )savePendingUpdate : (NSString *)packageHash
222
- wasInitialized : (BOOL )wasInitialized
226
+ isLoading : (BOOL )isLoading
223
227
{
224
228
// Since we're not restarting, we need to store the fact that the update
225
229
// was installed, but hasn't yet become "active".
226
230
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults ];
227
231
NSDictionary *pendingUpdate = [[NSDictionary alloc ] initWithObjectsAndKeys:
228
232
packageHash,PendingUpdateHashKey,
229
- [NSNumber numberWithBool: wasInitialized],PendingUpdateWasInitializedKey , nil ];
233
+ [NSNumber numberWithBool: isLoading],PendingUpdateIsLoadingKey , nil ];
230
234
231
235
[preferences setObject: pendingUpdate forKey: PendingUpdateKey];
232
236
[preferences synchronize ];
@@ -315,7 +319,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
315
319
reject (error);
316
320
} else {
317
321
[self savePendingUpdate: updatePackage[@" packageHash" ]
318
- wasInitialized :NO ];
322
+ isLoading :NO ];
319
323
320
324
if (installMode == CodePushInstallModeImmediate) {
321
325
[self loadBundle ];
0 commit comments