summaryrefslogtreecommitdiff
path: root/platform/ios/src/MGLMapboxEvents.m
diff options
context:
space:
mode:
authorJesse Bounds <jesse@rebounds.net>2016-10-18 14:32:32 -0700
committerJason Wray <jason@mapbox.com>2016-10-25 22:39:26 -0700
commit32be7b6cc389af6d4a83b9795bd0c7ceb2edd228 (patch)
tree4333822cd71e3bdb4f7e4a2a791608edbde16984 /platform/ios/src/MGLMapboxEvents.m
parent36738c00259572402909c3862420c17ad980fe25 (diff)
downloadqtlocation-mapboxgl-32be7b6cc389af6d4a83b9795bd0c7ceb2edd228.tar.gz
[ios] Optimize event uploading (#6737)
Upload any events collected when the app was in use for apps that only have "when in use" location permissions. Also, for all apps, avoid posting event data if there is only a single event.
Diffstat (limited to 'platform/ios/src/MGLMapboxEvents.m')
-rw-r--r--platform/ios/src/MGLMapboxEvents.m27
1 files changed, 22 insertions, 5 deletions
diff --git a/platform/ios/src/MGLMapboxEvents.m b/platform/ios/src/MGLMapboxEvents.m
index 53b917f701..4d62e9d724 100644
--- a/platform/ios/src/MGLMapboxEvents.m
+++ b/platform/ios/src/MGLMapboxEvents.m
@@ -129,6 +129,7 @@ const NSTimeInterval MGLFlushInterval = 180;
@implementation MGLMapboxEvents {
NSString *_instanceID;
+ UIBackgroundTaskIdentifier _backgroundTaskIdentifier;
}
+ (void)initialize {
@@ -257,9 +258,20 @@ const NSTimeInterval MGLFlushInterval = 180;
}
- (void)pauseOrResumeMetricsCollectionIfRequired {
+ UIApplication *application = [UIApplication sharedApplication];
+
// Prevent blue status bar when host app has `when in use` permission only and it is not in foreground
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse &&
- [UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
+ application.applicationState == UIApplicationStateBackground) {
+
+ if (_backgroundTaskIdentifier == UIBackgroundTaskInvalid) {
+ _backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
+ [application endBackgroundTask:_backgroundTaskIdentifier];
+ _backgroundTaskIdentifier = UIBackgroundTaskInvalid;
+ }];
+ [self flush];
+ }
+
[self pauseMetricsCollection];
return;
}
@@ -307,7 +319,10 @@ const NSTimeInterval MGLFlushInterval = 180;
return;
}
- if ([self.eventQueue count] == 0) {
+ if ([self.eventQueue count] <= 1) {
+ [self.eventQueue removeAllObjects];
+ [[UIApplication sharedApplication] endBackgroundTask:_backgroundTaskIdentifier];
+ _backgroundTaskIdentifier = UIBackgroundTaskInvalid;
return;
}
@@ -485,10 +500,12 @@ const NSTimeInterval MGLFlushInterval = 180;
if (error) {
[strongSelf pushDebugEvent:MGLEventTypeLocalDebug withAttributes:@{MGLEventKeyLocalDebugDescription: @"Network error",
@"error": error}];
- return;
+ } else {
+ [strongSelf pushDebugEvent:MGLEventTypeLocalDebug withAttributes:@{MGLEventKeyLocalDebugDescription: @"post",
+ @"debug.eventsCount": @(events.count)}];
}
- [strongSelf pushDebugEvent:MGLEventTypeLocalDebug withAttributes:@{MGLEventKeyLocalDebugDescription: @"post",
- @"debug.eventsCount": @(events.count)}];
+ [[UIApplication sharedApplication] endBackgroundTask:_backgroundTaskIdentifier];
+ _backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}];
});
}