@@ -643,28 +643,38 @@ - (void)savePendingUpdate:(NSString *)packageHash
643
643
return @[DownloadProgressEvent];
644
644
}
645
645
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
651
648
{
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 ;
655
650
if (_lastResignedDate) {
656
- durationInBackground = [[NSDate date ] timeIntervalSinceDate: _lastResignedDate];
651
+ duration = [[NSDate date ] timeIntervalSinceDate: _lastResignedDate];
657
652
}
658
653
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
+ {
659
663
if (_installMode == CodePushInstallModeOnNextSuspend) {
664
+ int durationInBackground = [self getDurationInBackground ];
660
665
// We shouldn't use loadBundle in this case, because _appSuspendTimer will call loadBundleOnTick.
661
666
// We should cancel timer for _appSuspendTimer because otherwise, we would call loadBundle two times.
662
667
if (durationInBackground < _minimumBackgroundDuration) {
663
668
[_appSuspendTimer invalidate ];
664
669
_appSuspendTimer = nil ;
665
670
}
666
- } else {
667
- // For resume install mode.
671
+ }
672
+ }
673
+
674
+ - (void )applicationWillEnterForeground
675
+ {
676
+ if (_installMode == CodePushInstallModeOnNextResume) {
677
+ int durationInBackground = [self getDurationInBackground ];
668
678
if (durationInBackground >= _minimumBackgroundDuration) {
669
679
[self restartAppInternal: NO ];
670
680
}
@@ -899,6 +909,11 @@ - (void)restartAppInternal:(BOOL)onlyIfUpdateIsPending
899
909
// Ensure we do not add the listener twice.
900
910
// Register for app resume notifications so that we
901
911
// 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
+
902
917
[[NSNotificationCenter defaultCenter ] addObserver: self
903
918
selector: @selector (applicationWillEnterForeground )
904
919
name: UIApplicationWillEnterForegroundNotification
0 commit comments