@@ -767,81 +767,83 @@ - (void) applicationDidEnterBackground {
767
767
// Record the time at which the app goes to the background.
768
768
self->_appLastBackgrounded = mach_absolute_time ();
769
769
// 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
+ }
776
779
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" );
789
784
return ;
790
785
}
791
786
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 ) {
796
789
return ;
797
790
}
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];
819
792
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
820
793
@try {
821
794
#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 ]];
827
799
#ifndef DISABLE_NRMA_EXCEPTION_WRAPPER
828
800
} @catch (NSException *exception) {
829
801
[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 )];
838
804
}
839
805
#endif
840
- NRLOG_AGENT_VERBOSE (@" Background Harvest Complete" );
841
806
}
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
+ }
843
827
#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 ]) {
844
836
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
+
845
847
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT,
846
848
0 ),
847
849
^{
@@ -913,7 +915,6 @@ - (void) applicationDidEnterBackground {
913
915
#endif
914
916
}
915
917
});
916
- #if !TARGET_OS_WATCH
917
918
} else {
918
919
[NRMAHarvestController stop ];
919
920
NRLOG_AGENT_ERROR (@" Multitasking is not supported. Clearing data." );
0 commit comments