From 4ad8f4a163fc178118e447bfc7538057e0f26a67 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Fri, 8 Sep 2023 17:02:59 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E3=80=90iOS=E3=80=91Fix=20timer=20backgrou?= =?UTF-8?q?nd=20state=20when=20App=20is=20launched=20from=20background?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/react-native/React/CoreModules/RCTTiming.mm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/react-native/React/CoreModules/RCTTiming.mm b/packages/react-native/React/CoreModules/RCTTiming.mm index 6065006b8eb281..91d94bf372e034 100644 --- a/packages/react-native/React/CoreModules/RCTTiming.mm +++ b/packages/react-native/React/CoreModules/RCTTiming.mm @@ -127,7 +127,9 @@ - (void)setup { _paused = YES; _timers = [NSMutableDictionary new]; - _inBackground = NO; + RCTUnsafeExecuteOnMainQueueSync(^{ + _inBackground = [UIApplication sharedApplication].applicationState == UIApplicationStateBackground; + }); for (NSString *name in @[ UIApplicationWillResignActiveNotification, From 3f1a9847954c8799126391a3edc0b615af098570 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Fri, 8 Sep 2023 22:35:25 +0800 Subject: [PATCH 2/5] Add self in block --- packages/react-native/React/CoreModules/RCTTiming.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-native/React/CoreModules/RCTTiming.mm b/packages/react-native/React/CoreModules/RCTTiming.mm index 91d94bf372e034..a3766be326880f 100644 --- a/packages/react-native/React/CoreModules/RCTTiming.mm +++ b/packages/react-native/React/CoreModules/RCTTiming.mm @@ -128,7 +128,7 @@ - (void)setup _paused = YES; _timers = [NSMutableDictionary new]; RCTUnsafeExecuteOnMainQueueSync(^{ - _inBackground = [UIApplication sharedApplication].applicationState == UIApplicationStateBackground; + self->_inBackground = [UIApplication sharedApplication].applicationState == UIApplicationStateBackground; }); for (NSString *name in @[ From 2a1f2fec0a08efa21b61f862e92f802ab463cb3a Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Sat, 9 Sep 2023 00:04:08 +0800 Subject: [PATCH 3/5] Change sync to async --- packages/react-native/React/CoreModules/RCTTiming.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTTiming.mm b/packages/react-native/React/CoreModules/RCTTiming.mm index a3766be326880f..3429d66ed06e39 100644 --- a/packages/react-native/React/CoreModules/RCTTiming.mm +++ b/packages/react-native/React/CoreModules/RCTTiming.mm @@ -127,8 +127,8 @@ - (void)setup { _paused = YES; _timers = [NSMutableDictionary new]; - RCTUnsafeExecuteOnMainQueueSync(^{ - self->_inBackground = [UIApplication sharedApplication].applicationState == UIApplicationStateBackground; + dispatch_async(dispatch_get_main_queue(), ^{ + self->_inBackground = ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground); }); for (NSString *name in @[ From f0ea810eec3b8793344a804711d7845dd5a7af15 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Sat, 9 Sep 2023 15:43:50 +0800 Subject: [PATCH 4/5] Trigger appDidMoveToBackground if we find app is in background --- packages/react-native/React/CoreModules/RCTTiming.mm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/react-native/React/CoreModules/RCTTiming.mm b/packages/react-native/React/CoreModules/RCTTiming.mm index 3429d66ed06e39..d66ddd46629386 100644 --- a/packages/react-native/React/CoreModules/RCTTiming.mm +++ b/packages/react-native/React/CoreModules/RCTTiming.mm @@ -127,8 +127,11 @@ - (void)setup { _paused = YES; _timers = [NSMutableDictionary new]; + _inBackground = NO; dispatch_async(dispatch_get_main_queue(), ^{ - self->_inBackground = ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground); + if (!self->_inBackground && [UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { + [self appDidMoveToBackground]; + } }); for (NSString *name in @[ From 0ebd574283eb3542116438522fdf382bd8815ed4 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Sat, 9 Sep 2023 17:52:26 +0800 Subject: [PATCH 5/5] Change to RN code style --- packages/react-native/React/CoreModules/RCTTiming.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTTiming.mm b/packages/react-native/React/CoreModules/RCTTiming.mm index d66ddd46629386..5158741724194b 100644 --- a/packages/react-native/React/CoreModules/RCTTiming.mm +++ b/packages/react-native/React/CoreModules/RCTTiming.mm @@ -128,8 +128,8 @@ - (void)setup _paused = YES; _timers = [NSMutableDictionary new]; _inBackground = NO; - dispatch_async(dispatch_get_main_queue(), ^{ - if (!self->_inBackground && [UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { + RCTExecuteOnMainQueue(^{ + if (!self->_inBackground && [RCTSharedApplication() applicationState] == UIApplicationStateBackground) { [self appDidMoveToBackground]; } });