Skip to content

Commit 8ce2abc

Browse files
authored
NR-308480 changed the #if so that NSProcessInfo is used for watchOS and not for iOS/tvOS (#317)
1 parent e66a6dd commit 8ce2abc

File tree

1 file changed

+60
-59
lines changed

1 file changed

+60
-59
lines changed

Agent/General/NewRelicAgentInternal.m

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -767,81 +767,83 @@ - (void) applicationDidEnterBackground {
767767
// Record the time at which the app goes to the background.
768768
self->_appLastBackgrounded = mach_absolute_time();
769769
// Check if the iOS version supports multitasking.
770-
#if !TARGET_OS_WATCH
771-
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)] &&
772-
[[UIDevice currentDevice] isMultitaskingSupported]) {
773-
774-
// Current One time background task is dispatched upon going to the background.
775-
UIApplication* application = [UIApplication sharedApplication];
770+
#if TARGET_OS_WATCH
771+
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
772+
// Mark the start of the background task
773+
[processInfo performExpiringActivityWithReason:@"harvestOnAppBackground"
774+
usingBlock:^(BOOL expired) {
775+
if(expired) {
776+
NRLOG_AGENT_VERBOSE(@"Unable to send watchOS OnAppBackground harvest - activity expired");
777+
return;
778+
}
776779

777-
// Mark the start of our background task.
778-
background_task = [application beginBackgroundTaskWithExpirationHandler:^{
779-
// This handler fires when our remaining background time approaches 0. We should not get here normally.
780-
[application endBackgroundTask:background_task];
781-
background_task = UIBackgroundTaskInvalid;
782-
}];
783-
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
784-
// Mark the start of the background task
785-
[processInfo performExpiringActivityWithReason:@"harvestOnAppBackground"
786-
usingBlock:^(BOOL expired) {
787-
if(expired) {
788-
NRLOG_AGENT_VERBOSE(@"Unable to send watchOS OnAppBackground harvest - activity expired");
780+
@synchronized (kNRMA_BGFG_MUTEX) {
781+
if(didFireEnterForeground) {
782+
// Entered foreground before we could finish background harvest
783+
NRLOG_AGENT_VERBOSE(@"Entered Foreground before background could complete. Bailing out of background logging");
789784
return;
790785
}
791786

792-
@synchronized (kNRMA_BGFG_MUTEX) {
793-
if(didFireEnterForeground) {
794-
// Entered foreground before we could finish background harvest
795-
NRLOG_AGENT_VERBOSE(@"Entered Foreground before background could complete. Bailing out of background logging");
787+
@synchronized (kNRMA_APPLICATION_WILL_TERMINATE) {
788+
if(self.appWillTerminate) {
796789
return;
797790
}
798-
799-
@synchronized (kNRMA_APPLICATION_WILL_TERMINATE) {
800-
if(self.appWillTerminate) {
801-
return;
802-
}
803-
NSTimeInterval sessionLength = [[NSDate date] timeIntervalSinceDate:self.appSessionStartDate];
804-
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
805-
@try {
806-
#endif
807-
self.gestureFacade = nil;
808-
[self.analyticsController sessionWillEnd];
809-
[NRMATaskQueue queue:[[NRMAMetric alloc] initWithName:@"Session/Duration"
810-
value:[NSNumber numberWithDouble:sessionLength] scope:nil]];
811-
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
812-
} @catch (NSException *exception) {
813-
[NRMAExceptionHandler logException:exception
814-
class:NSStringFromClass([self class])
815-
selector:NSStringFromSelector(_cmd)];
816-
}
817-
#endif
818-
}
791+
NSTimeInterval sessionLength = [[NSDate date] timeIntervalSinceDate:self.appSessionStartDate];
819792
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
820793
@try {
821794
#endif
822-
if(self.appWillTerminate) {
823-
return;
824-
}
825-
NRLOG_AGENT_VERBOSE(@"Harvesting data in background");
826-
[[[NRMAHarvestController harvestController] harvester] execute];
795+
self.gestureFacade = nil;
796+
[self.analyticsController sessionWillEnd];
797+
[NRMATaskQueue queue:[[NRMAMetric alloc] initWithName:@"Session/Duration"
798+
value:[NSNumber numberWithDouble:sessionLength] scope:nil]];
827799
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
828800
} @catch (NSException *exception) {
829801
[NRMAExceptionHandler logException:exception
830-
class:NSStringFromClass([NRMAHarvester class])
831-
selector:@"execute"];
832-
} @finally {
833-
if ([NRMAFlags shouldEnableBackgroundReporting]) {
834-
NRLOG_VERBOSE(@"Not calling agentShutdown since BackgroundInstrumentation is enabled.");
835-
} else {
836-
[self agentShutdown];
837-
}
802+
class:NSStringFromClass([self class])
803+
selector:NSStringFromSelector(_cmd)];
838804
}
839805
#endif
840-
NRLOG_AGENT_VERBOSE(@"Background Harvest Complete");
841806
}
842-
}];
807+
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
808+
@try {
809+
#endif
810+
if(self.appWillTerminate) {
811+
return;
812+
}
813+
NRLOG_AGENT_VERBOSE(@"Harvesting data in background");
814+
[[[NRMAHarvestController harvestController] harvester] execute];
815+
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
816+
} @catch (NSException *exception) {
817+
[NRMAExceptionHandler logException:exception
818+
class:NSStringFromClass([NRMAHarvester class])
819+
selector:@"execute"];
820+
} @finally {
821+
if ([NRMAFlags shouldEnableBackgroundReporting]) {
822+
NRLOG_VERBOSE(@"Not calling agentShutdown since BackgroundInstrumentation is enabled.");
823+
} else {
824+
[self agentShutdown];
825+
}
826+
}
843827
#endif
828+
NRLOG_AGENT_VERBOSE(@"Background Harvest Complete");
829+
}
830+
}];
831+
832+
#else
833+
834+
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)] &&
835+
[[UIDevice currentDevice] isMultitaskingSupported]) {
844836

837+
// Current One time background task is dispatched upon going to the background.
838+
UIApplication* application = [UIApplication sharedApplication];
839+
840+
// Mark the start of our background task.
841+
background_task = [application beginBackgroundTaskWithExpirationHandler:^{
842+
// This handler fires when our remaining background time approaches 0. We should not get here normally.
843+
[application endBackgroundTask:background_task];
844+
background_task = UIBackgroundTaskInvalid;
845+
}];
846+
845847
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
846848
0),
847849
^{
@@ -913,7 +915,6 @@ - (void) applicationDidEnterBackground {
913915
#endif
914916
}
915917
});
916-
#if !TARGET_OS_WATCH
917918
} else {
918919
[NRMAHarvestController stop];
919920
NRLOG_AGENT_ERROR(@"Multitasking is not supported. Clearing data.");

0 commit comments

Comments
 (0)