Use background task when sending analytics events.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 543c05b2c5
commit 11f52757b2

@ -124,15 +124,15 @@ const int kOWSAnalytics_DiscardFrequency = 0;
- (void)tryToSyncEvents - (void)tryToSyncEvents
{ {
// Don't try to sync if:
//
// * There's no network available.
// * There's already a sync request in flight.
if (!self.reachability.isReachable || self.hasRequestInFlight) {
return;
}
dispatch_async(self.serialQueue, ^{ dispatch_async(self.serialQueue, ^{
// Don't try to sync if:
//
// * There's no network available.
// * There's already a sync request in flight.
if (!self.reachability.isReachable || self.hasRequestInFlight) {
return;
}
__block NSString *firstEventKey = nil; __block NSString *firstEventKey = nil;
__block NSDictionary *firstEventDictionary = nil; __block NSDictionary *firstEventDictionary = nil;
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
@ -157,6 +157,12 @@ const int kOWSAnalytics_DiscardFrequency = 0;
DDLogDebug(@"%@ trying to deliver event: %@", self.tag, firstEventKey); DDLogDebug(@"%@ trying to deliver event: %@", self.tag, firstEventKey);
self.hasRequestInFlight = YES; self.hasRequestInFlight = YES;
__block UIBackgroundTaskIdentifier task;
task = [UIApplication.sharedApplication beginBackgroundTaskWithExpirationHandler:^{
[UIApplication.sharedApplication endBackgroundTask:task];
}];
// Until we integrate with an analytics platform, behave as though all event delivery succeeds. // Until we integrate with an analytics platform, behave as though all event delivery succeeds.
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
self.hasRequestInFlight = NO; self.hasRequestInFlight = NO;
@ -169,6 +175,8 @@ const int kOWSAnalytics_DiscardFrequency = 0;
}]; }];
} }
[UIApplication.sharedApplication endBackgroundTask:task];
// Wait a second between network requests / retries. // Wait a second between network requests / retries.
dispatch_after( dispatch_after(
dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

Loading…
Cancel
Save