Skip to content

Commit

Permalink
NR-308480 changed the #if so that NSProcessInfo is used for watchOS a…
Browse files Browse the repository at this point in the history
…nd not for iOS/tvOS (#317)
  • Loading branch information
mbruin-NR authored Oct 11, 2024
1 parent e66a6dd commit 8ce2abc
Showing 1 changed file with 60 additions and 59 deletions.
119 changes: 60 additions & 59 deletions Agent/General/NewRelicAgentInternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -767,81 +767,83 @@ - (void) applicationDidEnterBackground {
// Record the time at which the app goes to the background.
self->_appLastBackgrounded = mach_absolute_time();
// Check if the iOS version supports multitasking.
#if !TARGET_OS_WATCH
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)] &&
[[UIDevice currentDevice] isMultitaskingSupported]) {

// Current One time background task is dispatched upon going to the background.
UIApplication* application = [UIApplication sharedApplication];
#if TARGET_OS_WATCH
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
// Mark the start of the background task
[processInfo performExpiringActivityWithReason:@"harvestOnAppBackground"
usingBlock:^(BOOL expired) {
if(expired) {
NRLOG_AGENT_VERBOSE(@"Unable to send watchOS OnAppBackground harvest - activity expired");
return;
}

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

@synchronized (kNRMA_BGFG_MUTEX) {
if(didFireEnterForeground) {
// Entered foreground before we could finish background harvest
NRLOG_AGENT_VERBOSE(@"Entered Foreground before background could complete. Bailing out of background logging");
@synchronized (kNRMA_APPLICATION_WILL_TERMINATE) {
if(self.appWillTerminate) {
return;
}

@synchronized (kNRMA_APPLICATION_WILL_TERMINATE) {
if(self.appWillTerminate) {
return;
}
NSTimeInterval sessionLength = [[NSDate date] timeIntervalSinceDate:self.appSessionStartDate];
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
@try {
#endif
self.gestureFacade = nil;
[self.analyticsController sessionWillEnd];
[NRMATaskQueue queue:[[NRMAMetric alloc] initWithName:@"Session/Duration"
value:[NSNumber numberWithDouble:sessionLength] scope:nil]];
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
} @catch (NSException *exception) {
[NRMAExceptionHandler logException:exception
class:NSStringFromClass([self class])
selector:NSStringFromSelector(_cmd)];
}
#endif
}
NSTimeInterval sessionLength = [[NSDate date] timeIntervalSinceDate:self.appSessionStartDate];
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
@try {
#endif
if(self.appWillTerminate) {
return;
}
NRLOG_AGENT_VERBOSE(@"Harvesting data in background");
[[[NRMAHarvestController harvestController] harvester] execute];
self.gestureFacade = nil;
[self.analyticsController sessionWillEnd];
[NRMATaskQueue queue:[[NRMAMetric alloc] initWithName:@"Session/Duration"
value:[NSNumber numberWithDouble:sessionLength] scope:nil]];
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
} @catch (NSException *exception) {
[NRMAExceptionHandler logException:exception
class:NSStringFromClass([NRMAHarvester class])
selector:@"execute"];
} @finally {
if ([NRMAFlags shouldEnableBackgroundReporting]) {
NRLOG_VERBOSE(@"Not calling agentShutdown since BackgroundInstrumentation is enabled.");
} else {
[self agentShutdown];
}
class:NSStringFromClass([self class])
selector:NSStringFromSelector(_cmd)];
}
#endif
NRLOG_AGENT_VERBOSE(@"Background Harvest Complete");
}
}];
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
@try {
#endif
if(self.appWillTerminate) {
return;
}
NRLOG_AGENT_VERBOSE(@"Harvesting data in background");
[[[NRMAHarvestController harvestController] harvester] execute];
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
} @catch (NSException *exception) {
[NRMAExceptionHandler logException:exception
class:NSStringFromClass([NRMAHarvester class])
selector:@"execute"];
} @finally {
if ([NRMAFlags shouldEnableBackgroundReporting]) {
NRLOG_VERBOSE(@"Not calling agentShutdown since BackgroundInstrumentation is enabled.");
} else {
[self agentShutdown];
}
}
#endif
NRLOG_AGENT_VERBOSE(@"Background Harvest Complete");
}
}];

#else

if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)] &&
[[UIDevice currentDevice] isMultitaskingSupported]) {

// Current One time background task is dispatched upon going to the background.
UIApplication* application = [UIApplication sharedApplication];

// Mark the start of our background task.
background_task = [application beginBackgroundTaskWithExpirationHandler:^{
// This handler fires when our remaining background time approaches 0. We should not get here normally.
[application endBackgroundTask:background_task];
background_task = UIBackgroundTaskInvalid;
}];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0),
^{
Expand Down Expand Up @@ -913,7 +915,6 @@ - (void) applicationDidEnterBackground {
#endif
}
});
#if !TARGET_OS_WATCH
} else {
[NRMAHarvestController stop];
NRLOG_AGENT_ERROR(@"Multitasking is not supported. Clearing data.");
Expand Down

0 comments on commit 8ce2abc

Please sign in to comment.