Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS: ON_NEXT_SUSPEND - cancel the timer in applicationDidBecomeActive method #1991

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions ios/CodePush/CodePush.m
Original file line number Diff line number Diff line change
Expand Up @@ -643,28 +643,38 @@ - (void)savePendingUpdate:(NSString *)packageHash
return @[DownloadProgressEvent];
}

#pragma mark - Application lifecycle event handlers

// These two handlers will only be registered when there is
// a resume-based update still pending installation.
- (void)applicationWillEnterForeground
// Determine how long the app was in the background
- (int)getDurationInBackground
{
// Determine how long the app was in the background and ensure
// that it meets the minimum duration amount of time.
int durationInBackground = 0;
int duration = 0;
if (_lastResignedDate) {
durationInBackground = [[NSDate date] timeIntervalSinceDate:_lastResignedDate];
duration = [[NSDate date] timeIntervalSinceDate:_lastResignedDate];
}

return duration;
}

#pragma mark - Application lifecycle event handlers

// These three handlers will only be registered when there is
// a resume-based update still pending installation.
- (void)applicationDidBecomeActive
{
if (_installMode == CodePushInstallModeOnNextSuspend) {
int durationInBackground = [self getDurationInBackground];
// We shouldn't use loadBundle in this case, because _appSuspendTimer will call loadBundleOnTick.
// We should cancel timer for _appSuspendTimer because otherwise, we would call loadBundle two times.
if (durationInBackground < _minimumBackgroundDuration) {
[_appSuspendTimer invalidate];
_appSuspendTimer = nil;
}
} else {
// For resume install mode.
}
}

- (void)applicationWillEnterForeground
{
if (_installMode == CodePushInstallModeOnNextResume) {
int durationInBackground = [self getDurationInBackground];
if (durationInBackground >= _minimumBackgroundDuration) {
[self restartAppInternal:NO];
}
Expand Down Expand Up @@ -899,6 +909,11 @@ - (void)restartAppInternal:(BOOL)onlyIfUpdateIsPending
// Ensure we do not add the listener twice.
// Register for app resume notifications so that we
// can check for pending updates which support "restart on resume"
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:RCTSharedApplication()];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification
Expand Down