Permalink
Browse files

fix background issues

  • Loading branch information...
1 parent 8e97f57 commit 38e643a9dd5713c12a844f309d5cf9ffdd1ac2d3 @neilrahilly neilrahilly committed Oct 18, 2012
Showing with 37 additions and 11 deletions.
  1. +37 −11 Mixpanel/Mixpanel.m
View
@@ -123,6 +123,14 @@ + (NSString *)appVersion
return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
}
++ (BOOL)inBackground
+{
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 40000
+ return [[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground;
+#endif
+ return false;
+}
+
+ (NSDictionary *)interfaces
{
NSMutableDictionary *theDictionary = [NSMutableDictionary dictionary];
@@ -318,6 +326,9 @@ - (void)track:(NSString *)event properties:(NSDictionary *)properties
NSDictionary *e = [NSDictionary dictionaryWithObjectsAndKeys:event, @"event", [NSDictionary dictionaryWithDictionary:p], @"properties", nil];
DebugLog(@"%@ queueing event: %@", self, e);
[self.eventsQueue addObject:e];
+ if ([Mixpanel inBackground]) {
+ [self archiveEvents];
+ }
}
}
@@ -328,6 +339,9 @@ - (void)registerSuperProperties:(NSDictionary *)properties
[Mixpanel assertPropertyTypes:properties];
@synchronized(self) {
[self.superProperties addEntriesFromDictionary:properties];
+ if ([Mixpanel inBackground]) {
+ [self archiveProperties];
+ }
}
}
@@ -340,6 +354,9 @@ - (void)registerSuperPropertiesOnce:(NSDictionary *)properties
[self.superProperties setObject:[properties objectForKey:key] forKey:key];
}
}
+ if ([Mixpanel inBackground]) {
+ [self archiveProperties];
+ }
}
}
@@ -353,13 +370,19 @@ - (void)registerSuperPropertiesOnce:(NSDictionary *)properties defaultValue:(id)
[self.superProperties setObject:[properties objectForKey:key] forKey:key];
}
}
+ if ([Mixpanel inBackground]) {
+ [self archiveProperties];
+ }
}
}
- (void)clearSuperProperties
{
@synchronized(self) {
[self.superProperties removeAllObjects];
+ if ([Mixpanel inBackground]) {
+ [self archiveProperties];
+ }
}
}
@@ -707,20 +730,17 @@ - (void)applicationDidEnterBackground:(NSNotificationCenter *)notification
- (void)applicationWillEnterForeground:(NSNotificationCenter *)notification
{
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 40000
@synchronized(self) {
- [self unarchive];
-
-#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 40000
if (&UIBackgroundTaskInvalid) {
if (self.taskId != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:self.taskId];
}
self.taskId = UIBackgroundTaskInvalid;
}
-#endif
-
}
+#endif
}
- (void)applicationWillTerminate:(NSNotification *)notification
@@ -731,20 +751,20 @@ - (void)applicationWillTerminate:(NSNotification *)notification
}
}
-- (void)endTaskIfInBackground
+- (void)endBackgroundTaskIfComplete
{
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 40000
// if the os version allows background tasks, the app supports them, and we're in one, end it
@synchronized(self) {
-#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 40000
if (&UIBackgroundTaskInvalid && [[UIApplication sharedApplication] respondsToSelector:@selector(endBackgroundTask:)] &&
self.taskId != UIBackgroundTaskInvalid && self.eventsConnection == nil && self.peopleConnection == nil) {
DevLog(@"%@ ending background task", self);
[[UIApplication sharedApplication] endBackgroundTask:self.taskId];
self.taskId = UIBackgroundTaskInvalid;
}
-#endif
}
+#endif
}
#pragma mark * NSURLConnection callbacks
@@ -799,7 +819,7 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err
[self updateNetworkActivityIndicator];
- [self endTaskIfInBackground];
+ [self endBackgroundTaskIfComplete];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
@@ -836,7 +856,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection
[self updateNetworkActivityIndicator];
- [self endTaskIfInBackground];
+ [self endBackgroundTaskIfComplete];
}
#pragma mark * NSObject
@@ -848,7 +868,6 @@ - (NSString *)description
- (void)dealloc
{
-// [self archive];
[self stopFlushTimer];
[self removeApplicationObservers];
@@ -909,6 +928,10 @@ - (void)identify:(NSString *)distinctId
}
[self.unidentifiedQueue removeAllObjects];
}
+ if ([Mixpanel inBackground]) {
+ [self.mixpanel archiveProperties];
+ [self.mixpanel archivePeople];
+ }
}
}
@@ -992,6 +1015,9 @@ - (void)addPeopleRecordToQueueWithAction:(NSString *)action andProperties:(NSDic
DebugLog(@"%@ queueing unidentified people record: %@", self.mixpanel, r);
[self.unidentifiedQueue addObject:r];
}
+ if ([Mixpanel inBackground]) {
+ [self.mixpanel archivePeople];
+ }
}
}

0 comments on commit 38e643a

Please sign in to comment.