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] Timing: Fixes timer when app get into background #24649

Closed
wants to merge 4 commits into from
Closed
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
30 changes: 30 additions & 0 deletions React/Modules/RCTTiming.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ @implementation RCTTiming
NSTimer *_sleepTimer;
BOOL _sendIdleEvents;
BOOL _inBackground;
UIBackgroundTaskIdentifier _backgroundTaskIdentifier;
}

@synthesize bridge = _bridge;
Expand All @@ -112,6 +113,7 @@ - (void)setBridge:(RCTBridge *)bridge
_paused = YES;
_timers = [NSMutableDictionary new];
_inBackground = NO;
_backgroundTaskIdentifier = UIBackgroundTaskInvalid;

for (NSString *name in @[UIApplicationWillResignActiveNotification,
UIApplicationDidEnterBackgroundNotification,
Expand All @@ -135,10 +137,35 @@ - (void)setBridge:(RCTBridge *)bridge

- (void)dealloc
{
[self markEndOfBackgroundTaskIfNeeded];
[_sleepTimer invalidate];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)markStartOfBackgroundTaskIfNeeded
{
if (_backgroundTaskIdentifier == UIBackgroundTaskInvalid) {
__weak typeof(self) weakSelf = self;
// Marks the beginning of a new long-running background task. We can run the timer in the background.
_backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
typeof(self) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
// Mark the end of background task
[strongSelf markEndOfBackgroundTaskIfNeeded];
}];
}
}

- (void)markEndOfBackgroundTaskIfNeeded
{
if (_backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:_backgroundTaskIdentifier];
_backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}
}

- (dispatch_queue_t)methodQueue
{
return RCTJSThread;
Expand All @@ -163,6 +190,7 @@ - (void)appDidMoveToBackground

- (void)appDidMoveToForeground
{
[self markEndOfBackgroundTaskIfNeeded];
_inBackground = NO;
[self startTimers];
}
Expand Down Expand Up @@ -260,6 +288,7 @@ - (void)didUpdateFrame:(RCTFrameUpdate *)update
}
if (_inBackground) {
if (timerCount) {
[self markStartOfBackgroundTaskIfNeeded];
[self scheduleSleepTimer:nextScheduledTarget];
}
} else if (!_sendIdleEvents && timersToCall.count == 0) {
Expand Down Expand Up @@ -338,6 +367,7 @@ - (void)timerDidFire
}

if (_inBackground) {
[self markStartOfBackgroundTaskIfNeeded];
[self scheduleSleepTimer:timer.target];
} else if (_paused) {
if ([timer.target timeIntervalSinceNow] > kMinimumSleepInterval) {
Expand Down