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

Commit 96c629d

Browse files
committed
extract clearDebugUpdates to seperate function
1 parent b0b9580 commit 96c629d

File tree

1 file changed

+55
-50
lines changed

1 file changed

+55
-50
lines changed

ios/CodePush/CodePush.m

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,32 @@ + (NSURL *)bundleURLForResource:(NSString *)resourceName
7676
{
7777
bundleResourceName = resourceName;
7878
bundleResourceExtension = resourceExtension;
79-
79+
8080
[self ensureBinaryBundleExists];
81-
81+
8282
NSString *logMessageFormat = @"Loading JS bundle from %@";
83-
83+
8484
NSError *error;
8585
NSString *packageFile = [CodePushPackage getCurrentPackageBundlePath:&error];
8686
NSURL *binaryBundleURL = [self binaryBundleURL];
87-
87+
8888
if (error || !packageFile) {
8989
NSLog(logMessageFormat, binaryBundleURL);
9090
isRunningBinaryVersion = YES;
9191
return binaryBundleURL;
9292
}
93-
93+
9494
NSString *binaryAppVersion = [[CodePushConfig current] appVersion];
9595
NSDictionary *currentPackageMetadata = [CodePushPackage getCurrentPackage:&error];
9696
if (error || !currentPackageMetadata) {
9797
NSLog(logMessageFormat, binaryBundleURL);
9898
isRunningBinaryVersion = YES;
9999
return binaryBundleURL;
100100
}
101-
101+
102102
NSString *packageDate = [currentPackageMetadata objectForKey:BinaryBundleDateKey];
103103
NSString *packageAppVersion = [currentPackageMetadata objectForKey:AppVersionKey];
104-
104+
105105
if ([[CodePushUpdateUtils modifiedDateStringOfFileAtURL:binaryBundleURL] isEqualToString:packageDate] && ([CodePush isUsingTestConfiguration] ||[binaryAppVersion isEqualToString:packageAppVersion])) {
106106
// Return package file because it is newer than the app store binary's JS bundle
107107
NSURL *packageUrl = [[NSURL alloc] initFileURLWithPath:packageFile];
@@ -113,11 +113,11 @@ + (NSURL *)bundleURLForResource:(NSString *)resourceName
113113
#ifndef DEBUG
114114
isRelease = YES;
115115
#endif
116-
116+
117117
if (isRelease || ![binaryAppVersion isEqualToString:packageAppVersion]) {
118118
[CodePush clearUpdates];
119119
}
120-
120+
121121
NSLog(logMessageFormat, binaryBundleURL);
122122
isRunningBinaryVersion = YES;
123123
return binaryBundleURL;
@@ -186,7 +186,7 @@ - (NSDictionary *)constantsToExport
186186
@"codePushInstallModeOnNextRestart":@(CodePushInstallModeOnNextRestart),
187187
@"codePushInstallModeImmediate": @(CodePushInstallModeImmediate),
188188
@"codePushInstallModeOnNextResume": @(CodePushInstallModeOnNextResume),
189-
189+
190190
@"codePushUpdateStateRunning": @(CodePushUpdateStateRunning),
191191
@"codePushUpdateStatePending": @(CodePushUpdateStatePending),
192192
@"codePushUpdateStateLatest": @(CodePushUpdateStateLatest)
@@ -208,44 +208,39 @@ + (void)ensureBinaryBundleExists
208208
{
209209
if (![self binaryBundleURL]) {
210210
NSString *errorMessage;
211-
211+
212212
#if TARGET_IPHONE_SIMULATOR
213213
errorMessage = @"React Native doesn't generate your app's JS bundle by default when deploying to the simulator. "
214214
"If you'd like to test CodePush using the simulator, you can do one of three things depending on your React "
215215
"Native version and/or preferred workflow:\n\n"
216-
216+
217217
"1. Update your AppDelegate.m file to load the JS bundle from the packager instead of from CodePush. "
218218
"You can still test your CodePush update experience using this workflow (debug builds only).\n\n"
219-
219+
220220
"2. Force the JS bundle to be generated in simulator builds by removing the if block that echoes "
221221
"\"Skipping bundling for Simulator platform\" in the \"node_modules/react-native/packager/react-native-xcode.sh\" file.\n\n"
222-
222+
223223
"3. Deploy a release build to the simulator, which unlike debug builds, will generate the JS bundle (React Native >=0.22.0 only).";
224224
#else
225225
errorMessage = [NSString stringWithFormat:@"The specified JS bundle file wasn't found within the app's binary. Is \"%@\" the correct file name?", [bundleResourceName stringByAppendingPathExtension:bundleResourceExtension]];
226226
#endif
227-
227+
228228
RCTFatal([CodePushErrorUtils errorWithMessage:errorMessage]);
229229
}
230230
}
231231

232232
- (instancetype)init
233233
{
234234
self = [super init];
235-
235+
236236
if (self) {
237237
[self initializeUpdateAfterRestart];
238238
}
239-
239+
240240
return self;
241241
}
242242

243-
/*
244-
* This method is used when the app is started to either
245-
* initialize a pending update or rollback a faulty update
246-
* to the previous version.
247-
*/
248-
- (void)initializeUpdateAfterRestart
243+
- (void)clearDebugUpdates
249244
{
250245
dispatch_async(dispatch_get_main_queue(), ^{
251246
if ([_bridge.bundleURL.scheme hasPrefix:@"http"]) {
@@ -260,6 +255,16 @@ - (void)initializeUpdateAfterRestart
260255
}
261256
}
262257
});
258+
}
259+
260+
/*
261+
* This method is used when the app is started to either
262+
* initialize a pending update or rollback a faulty update
263+
* to the previous version.
264+
*/
265+
- (void)initializeUpdateAfterRestart
266+
{
267+
[self clearDebugUpdates];
263268

264269
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
265270
NSDictionary *pendingUpdate = [preferences objectForKey:PendingUpdateKey];
@@ -305,7 +310,7 @@ - (BOOL)isFailedHash:(NSString*)packageHash
305310
}
306311
}
307312
}
308-
313+
309314
return NO;
310315
}
311316
}
@@ -319,13 +324,13 @@ - (BOOL)isPendingUpdate:(NSString*)packageHash
319324
{
320325
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
321326
NSDictionary *pendingUpdate = [preferences objectForKey:PendingUpdateKey];
322-
327+
323328
// If there is a pending update whose "state" isn't loading, then we consider it "pending".
324329
// Additionally, if a specific hash was provided, we ensure it matches that of the pending update.
325330
BOOL updateIsPending = pendingUpdate &&
326331
[pendingUpdate[PendingUpdateIsLoadingKey] boolValue] == NO &&
327332
(!packageHash || [pendingUpdate[PendingUpdateHashKey] isEqualToString:packageHash]);
328-
333+
329334
return updateIsPending;
330335
}
331336

@@ -346,7 +351,7 @@ - (void)loadBundle
346351
if ([CodePush isUsingTestConfiguration] || ![_bridge.bundleURL.scheme hasPrefix:@"http"]) {
347352
[_bridge setValue:[CodePush bundleURL] forKey:@"bundleURL"];
348353
}
349-
354+
350355
[_bridge reload];
351356
});
352357
}
@@ -362,10 +367,10 @@ - (void)rollbackPackage
362367
{
363368
NSError *error;
364369
NSDictionary *failedPackage = [CodePushPackage getCurrentPackage:&error];
365-
370+
366371
// Write the current package's metadata to the "failed list"
367372
[self saveFailedUpdate:failedPackage];
368-
373+
369374
// Rollback to the previous version and de-register the new update
370375
[CodePushPackage rollbackPackage];
371376
[CodePush removePendingUpdate];
@@ -388,7 +393,7 @@ - (void)saveFailedUpdate:(NSDictionary *)failedPackage
388393
// objects, regardless if you stored something mutable.
389394
failedUpdates = [failedUpdates mutableCopy];
390395
}
391-
396+
392397
[failedUpdates addObject:failedPackage];
393398
[preferences setObject:failedUpdates forKey:FailedUpdatesKey];
394399
[preferences synchronize];
@@ -430,7 +435,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
430435
NSDictionary *pendingUpdate = [[NSDictionary alloc] initWithObjectsAndKeys:
431436
packageHash,PendingUpdateHashKey,
432437
[NSNumber numberWithBool:isLoading],PendingUpdateIsLoadingKey, nil];
433-
438+
434439
[preferences setObject:pendingUpdate forKey:PendingUpdateKey];
435440
[preferences synchronize];
436441
}
@@ -471,7 +476,7 @@ - (void)applicationWillResignActive
471476
[mutableUpdatePackage setValue:[CodePushUpdateUtils modifiedDateStringOfFileAtURL:binaryBundleURL]
472477
forKey:BinaryBundleDateKey];
473478
}
474-
479+
475480
[CodePushPackage
476481
downloadPackage:mutableUpdatePackage
477482
expectedBundleFileName:[bundleResourceName stringByAppendingPathExtension:bundleResourceExtension]
@@ -492,11 +497,11 @@ - (void)applicationWillResignActive
492497
dispatch_async(_methodQueue, ^{
493498
NSError *err;
494499
NSDictionary *newPackage = [CodePushPackage getPackage:mutableUpdatePackage[PackageHashKey] error:&err];
495-
500+
496501
if (err) {
497502
return reject([NSString stringWithFormat: @"%lu", (long)err.code], err.localizedDescription, err);
498503
}
499-
504+
500505
resolve(newPackage);
501506
});
502507
}
@@ -506,7 +511,7 @@ - (void)applicationWillResignActive
506511
if ([CodePushErrorUtils isCodePushError:err]) {
507512
[self saveFailedUpdate:mutableUpdatePackage];
508513
}
509-
514+
510515
reject([NSString stringWithFormat: @"%lu", (long)err.code], err.localizedDescription, err);
511516
});
512517
}];
@@ -531,21 +536,21 @@ - (void)applicationWillResignActive
531536
resolve(configuration);
532537
return;
533538
}
534-
539+
535540
if (binaryHash == nil) {
536541
// The hash was not generated either due to a previous unknown error or the fact that
537542
// the React Native assets were not bundled in the binary (e.g. during dev/simulator)
538543
// builds.
539544
resolve(configuration);
540545
return;
541546
}
542-
547+
543548
NSMutableDictionary *mutableConfiguration = [configuration mutableCopy];
544549
[mutableConfiguration setObject:binaryHash forKey:PackageHashKey];
545550
resolve(mutableConfiguration);
546551
return;
547552
}
548-
553+
549554
resolve(configuration);
550555
}
551556

@@ -567,10 +572,10 @@ - (void)applicationWillResignActive
567572
// wanted to retrieve the pending or running update.
568573
return resolve(nil);
569574
}
570-
575+
571576
// We have a CodePush update, so let's see if it's currently in a pending state.
572577
BOOL currentUpdateIsPending = [self isPendingUpdate:[package objectForKey:PackageHashKey]];
573-
578+
574579
if (updateState == CodePushUpdateStatePending && !currentUpdateIsPending) {
575580
// The caller wanted a pending update
576581
// but there isn't currently one.
@@ -590,7 +595,7 @@ - (void)applicationWillResignActive
590595
// disk that is not actually running.
591596
[package setObject:@(YES) forKey:@"_isDebugOnly"];
592597
}
593-
598+
594599
// Enable differentiating pending vs. non-pending updates
595600
[package setObject:@(currentUpdateIsPending) forKey:PackageIsPendingKey];
596601
resolve(package);
@@ -610,16 +615,16 @@ - (void)applicationWillResignActive
610615
[CodePushPackage installPackage:updatePackage
611616
removePendingUpdate:[self isPendingUpdate:nil]
612617
error:&error];
613-
618+
614619
if (error) {
615620
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
616621
} else {
617622
[self savePendingUpdate:updatePackage[PackageHashKey]
618623
isLoading:NO];
619-
624+
620625
if (installMode == CodePushInstallModeOnNextResume) {
621626
_minimumBackgroundDuration = minimumBackgroundDuration;
622-
627+
623628
if (!_hasResumeListener) {
624629
// Ensure we do not add the listener twice.
625630
// Register for app resume notifications so that we
@@ -628,16 +633,16 @@ - (void)applicationWillResignActive
628633
selector:@selector(applicationWillEnterForeground)
629634
name:UIApplicationWillEnterForegroundNotification
630635
object:[UIApplication sharedApplication]];
631-
636+
632637
[[NSNotificationCenter defaultCenter] addObserver:self
633638
selector:@selector(applicationWillResignActive)
634639
name:UIApplicationWillResignActiveNotification
635640
object:[UIApplication sharedApplication]];
636-
641+
637642
_hasResumeListener = YES;
638643
}
639644
}
640-
645+
641646
// Signal to JS that the update has been applied.
642647
resolve(nil);
643648
}
@@ -668,7 +673,7 @@ - (void)applicationWillResignActive
668673
&& nil != packageHash
669674
&& [packageHash length] > 0
670675
&& [packageHash isEqualToString:[CodePushPackage getCurrentPackageHash:&error]];
671-
676+
672677
resolve(@(isFirstRun));
673678
}
674679

@@ -715,7 +720,7 @@ - (void)applicationWillResignActive
715720
*/
716721
RCT_EXPORT_METHOD(getNewStatusReport:(RCTPromiseResolveBlock)resolve
717722
rejecter:(RCTPromiseRejectBlock)reject)
718-
{
723+
{
719724
if (needToReportRollback) {
720725
needToReportRollback = NO;
721726
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
@@ -739,7 +744,7 @@ - (void)applicationWillResignActive
739744
resolve([CodePushTelemetryManager getBinaryUpdateReport:appVersion]);
740745
return;
741746
}
742-
747+
743748
resolve(nil);
744749
}
745750

0 commit comments

Comments
 (0)