Permalink
Browse files

fix broken connection issue

  • Loading branch information...
1 parent b38c7a6 commit cc9884eff21819495ad07da455f985792af1d21a @neilrahilly neilrahilly committed Oct 24, 2012
Showing with 50 additions and 10 deletions.
  1. +50 −10 Mixpanel/Mixpanel.m
View
@@ -171,9 +171,9 @@ + (NSString *)uniqueDeviceString
NSDictionary *dict = [Mixpanel interfaces];
NSArray *keys = [dict allKeys];
keys = [keys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
-
+
NSString *bundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:(id)kCFBundleNameKey];
-
+
// while most apps will define CFBundleName, it's not guaranteed;
// an app can choose to define it or not so when it's missing, use the bundle file name
if (bundleName == nil) {
@@ -467,9 +467,12 @@ - (void)flush
- (void)flushEvents
{
- if ([self.eventsQueue count] == 0 || self.eventsConnection != nil) {
+ if ([self.eventsQueue count] == 0) {
DevLog(@"%@ no events to flush", self);
return;
+ } else if (self.eventsConnection != nil) {
+ DevLog(@"%@ events connection already open", self);
+ return;
} else if ([self.eventsQueue count] > 50) {
self.eventsBatch = [self.eventsQueue subarrayWithRange:NSMakeRange(0, 50)];
} else {
@@ -482,15 +485,26 @@ - (void)flushEvents
DevLog(@"%@ flushing %u of %u queued events: %@", self, self.eventsBatch.count, self.eventsQueue.count, self.eventsQueue);
self.eventsConnection = [self apiConnectionWithEndpoint:@"/track/" andBody:postBody];
-
+
+ if(![NSThread isMainThread]){
+ DevLog(@"%@ keeping background events connection thread alive", self);
+ while(self.eventsConnection) {
+ [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
+ }
+ DevLog(@"%@ letting go of background events connection thread", self);
+ }
+
[self updateNetworkActivityIndicator];
}
- (void)flushPeople
{
- if ([self.peopleQueue count] == 0 || self.peopleConnection != nil) {
+ if ([self.peopleQueue count] == 0) {
DevLog(@"%@ no people to flush", self);
return;
+ } else if (self.peopleConnection != nil) {
+ DevLog(@"%@ people connection already open", self);
+ return;
} else if ([self.peopleQueue count] > 50) {
self.peopleBatch = [self.peopleQueue subarrayWithRange:NSMakeRange(0, 50)];
} else {
@@ -504,9 +518,35 @@ - (void)flushPeople
self.peopleConnection = [self apiConnectionWithEndpoint:@"/engage/" andBody:postBody];
+ if(![NSThread isMainThread]){
+ DevLog(@"%@ keeping background people connection thread alive", self);
+ while(self.peopleConnection) {
+ [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
+ }
+ DevLog(@"%@ letting go of background people connection thread", self);
+ }
+
[self updateNetworkActivityIndicator];
}
+- (void)cancelFlush
+{
+ if (self.eventsConnection == nil) {
+ DevLog(@"%@ no events connection to cancel", self);
+ } else {
+ DevLog(@"%@ cancelling events connection", self);
+ [self.eventsConnection cancel];
+ self.eventsConnection = nil;
+ }
+ if (self.peopleConnection == nil) {
+ DevLog(@"%@ no people connection to cancel", self);
+ } else {
+ DevLog(@"%@ cancelling people connection", self);
+ [self.peopleConnection cancel];
+ self.peopleConnection = nil;
+ }
+}
+
- (void)updateNetworkActivityIndicator
{
@synchronized(self) {
@@ -717,22 +757,20 @@ - (void)applicationDidEnterBackground:(NSNotificationCenter *)notification
self.taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
DevLog(@"%@ background flush cut short", self);
- [self.eventsConnection cancel];
- [self.peopleConnection cancel];
- self.eventsConnection = nil;
- self.peopleConnection = nil;
+ [self cancelFlush];
[[UIApplication sharedApplication] endBackgroundTask:self.taskId];
self.taskId = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
DevLog(@"%@ background flush starting", self);
[self flush];
+ DevLog(@"%@ ending background flush with task id: %u", self, self.taskId);
[[UIApplication sharedApplication] endBackgroundTask:self.taskId];
self.taskId = UIBackgroundTaskInvalid;
});
- DevLog(@"%@ background flush dispatched", self);
+ DevLog(@"%@ background flush dispatched with task id: %u", self, self.taskId);
}
#endif
}
@@ -749,6 +787,8 @@ - (void)applicationWillEnterForeground:(NSNotificationCenter *)notification
}
self.taskId = UIBackgroundTaskInvalid;
}
+ [self cancelFlush];
+ [self updateNetworkActivityIndicator];
}
#endif
}

0 comments on commit cc9884e

Please sign in to comment.