Permalink
Browse files

make flush on background optional

  • Loading branch information...
1 parent 7cb548e commit 13c6a182aad5c810585904ecd1fb8e5017cfddc5 @neilrahilly neilrahilly committed Oct 18, 2012
Showing with 36 additions and 17 deletions.
  1. +13 −0 Mixpanel/Mixpanel.h
  2. +23 −17 Mixpanel/Mixpanel.m
View
@@ -108,6 +108,19 @@
@property
@abstract
+ Control whether the library should flush data to Mixpanel when the app
+ enters the background.
+
+ @discussion
+ Defaults to YES. Only affects apps targeted at iOS 4.0, when background
+ task support was introduced, and later.
+ */
+@property(nonatomic,assign) BOOL flushOnBackground;
+
+/*!
+ @property
+
+ @abstract
Controls whether to show spinning network activity indicator when flushing data to the Mixpanel servers.
@discussion
View
@@ -257,6 +257,7 @@ - (id)initWithToken:(NSString *)apiToken andFlushInterval:(NSUInteger)flushInter
self.apiToken = apiToken;
self.flushInterval = flushInterval;
+ self.flushOnBackground = YES;
self.showNetworkActivityIndicator = YES;
self.serverURL = @"https://api.mixpanel.com";
@@ -643,26 +644,31 @@ - (void)applicationDidEnterBackground:(NSNotificationCenter *)notification
DevLog(@"%@ did enter background", self);
[self archive];
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 40000
- if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)] &&
- [[UIApplication sharedApplication] respondsToSelector:@selector(endBackgroundTask:)]) {
- DevLog(@"%@ background task supported", self);
- if (self.peopleQueue.count || self.eventsQueue.count) {
- DevLog(@"%@ background task start for queued items", self);
- self.taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
- DevLog(@"%@ background task expiration handler", self);
- [self.eventsConnection cancel];
- [self.peopleConnection cancel];
- self.eventsConnection = nil;
- self.peopleConnection = nil;
- [[UIApplication sharedApplication] endBackgroundTask:self.taskId];
- self.taskId = UIBackgroundTaskInvalid;
- }];
- [self flush];
+ if (self.flushOnBackground) {
+ DevLog(@"%@ background flush turned on", self);
+ if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)] &&
+ [[UIApplication sharedApplication] respondsToSelector:@selector(endBackgroundTask:)]) {
+ DevLog(@"%@ background task supported", self);
+ if (self.peopleQueue.count || self.eventsQueue.count) {
+ DevLog(@"%@ background task start for queued items", self);
+ self.taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
+ DevLog(@"%@ background task expiration handler", self);
+ [self.eventsConnection cancel];
+ [self.peopleConnection cancel];
+ self.eventsConnection = nil;
+ self.peopleConnection = nil;
+ [[UIApplication sharedApplication] endBackgroundTask:self.taskId];
+ self.taskId = UIBackgroundTaskInvalid;
+ }];
+ [self flush];
+ } else {
+ DevLog(@"%@ background task not needed", self);
+ }
} else {
- DevLog(@"%@ background task not needed", self);
+ DevLog(@"%@ background task not supported", self);
}
} else {
- DevLog(@"%@ background task not supported", self);
+ DevLog(@"%@ background flush turned off", self);
}
#endif
}

0 comments on commit 13c6a18

Please sign in to comment.