Skip to content

Commit b15a019

Browse files
author
Aleksandr Trutnev
authored
iOS: reset the timer in applicationDidBecomeActive method in case of using ON_NEXT_SUSPEND restart option (microsoft#1991)
1 parent 8614337 commit b15a019

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

ios/CodePush/CodePush.m

+26-11
Original file line numberDiff line numberDiff line change
@@ -643,28 +643,38 @@ - (void)savePendingUpdate:(NSString *)packageHash
643643
return @[DownloadProgressEvent];
644644
}
645645

646-
#pragma mark - Application lifecycle event handlers
647-
648-
// These two handlers will only be registered when there is
649-
// a resume-based update still pending installation.
650-
- (void)applicationWillEnterForeground
646+
// Determine how long the app was in the background
647+
- (int)getDurationInBackground
651648
{
652-
// Determine how long the app was in the background and ensure
653-
// that it meets the minimum duration amount of time.
654-
int durationInBackground = 0;
649+
int duration = 0;
655650
if (_lastResignedDate) {
656-
durationInBackground = [[NSDate date] timeIntervalSinceDate:_lastResignedDate];
651+
duration = [[NSDate date] timeIntervalSinceDate:_lastResignedDate];
657652
}
658653

654+
return duration;
655+
}
656+
657+
#pragma mark - Application lifecycle event handlers
658+
659+
// These three handlers will only be registered when there is
660+
// a resume-based update still pending installation.
661+
- (void)applicationDidBecomeActive
662+
{
659663
if (_installMode == CodePushInstallModeOnNextSuspend) {
664+
int durationInBackground = [self getDurationInBackground];
660665
// We shouldn't use loadBundle in this case, because _appSuspendTimer will call loadBundleOnTick.
661666
// We should cancel timer for _appSuspendTimer because otherwise, we would call loadBundle two times.
662667
if (durationInBackground < _minimumBackgroundDuration) {
663668
[_appSuspendTimer invalidate];
664669
_appSuspendTimer = nil;
665670
}
666-
} else {
667-
// For resume install mode.
671+
}
672+
}
673+
674+
- (void)applicationWillEnterForeground
675+
{
676+
if (_installMode == CodePushInstallModeOnNextResume) {
677+
int durationInBackground = [self getDurationInBackground];
668678
if (durationInBackground >= _minimumBackgroundDuration) {
669679
[self restartAppInternal:NO];
670680
}
@@ -899,6 +909,11 @@ - (void)restartAppInternal:(BOOL)onlyIfUpdateIsPending
899909
// Ensure we do not add the listener twice.
900910
// Register for app resume notifications so that we
901911
// can check for pending updates which support "restart on resume"
912+
[[NSNotificationCenter defaultCenter] addObserver:self
913+
selector:@selector(applicationDidBecomeActive)
914+
name:UIApplicationDidBecomeActiveNotification
915+
object:RCTSharedApplication()];
916+
902917
[[NSNotificationCenter defaultCenter] addObserver:self
903918
selector:@selector(applicationWillEnterForeground)
904919
name:UIApplicationWillEnterForegroundNotification

0 commit comments

Comments
 (0)