Skip to content

Commit

Permalink
make flush on background optional
Browse files Browse the repository at this point in the history
  • Loading branch information
neilrahilly committed Oct 18, 2012
1 parent 7cb548e commit 13c6a18
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
13 changes: 13 additions & 0 deletions Mixpanel/Mixpanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@
*/
@property(nonatomic,assign) NSUInteger flushInterval;

/*!
@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
Expand Down
40 changes: 23 additions & 17 deletions Mixpanel/Mixpanel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 13c6a18

Please sign in to comment.