Permalink
Browse files

fix threading bug (closes #36)

  • Loading branch information...
1 parent 38bfc0f commit d33b5fe004de7ef79f0135d292cc92221f03632b @neilrahilly neilrahilly committed Oct 31, 2012
Showing with 4 additions and 25 deletions.
  1. +4 −25 Mixpanel/Mixpanel.m
View
@@ -486,14 +486,6 @@ - (void)flushEvents
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];
}
@@ -518,14 +510,6 @@ - (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];
}
@@ -762,15 +746,10 @@ - (void)applicationDidEnterBackground:(NSNotificationCenter *)notification
self.taskId = UIBackgroundTaskInvalid;
}];
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- DevLog(@"%@ starting flush background task %u", self, self.taskId);
- [self flush];
- DevLog(@"%@ ending flush background task %u", self, self.taskId);
- [[UIApplication sharedApplication] endBackgroundTask:self.taskId];
- self.taskId = UIBackgroundTaskInvalid;
- });
+ DevLog(@"%@ starting flush background task %u", self, self.taskId);
+ [self flush];
- DevLog(@"%@ dispatched flush background task %u", self, self.taskId);
+ // connection callbacks end this task by calling endBackgroundTaskIfComplete
}
#endif
}
@@ -809,7 +788,7 @@ - (void)endBackgroundTaskIfComplete
if (&UIBackgroundTaskInvalid && [[UIApplication sharedApplication] respondsToSelector:@selector(endBackgroundTask:)] &&
self.taskId != UIBackgroundTaskInvalid && self.eventsConnection == nil && self.peopleConnection == nil) {
- DevLog(@"%@ ending background task", self);
+ DevLog(@"%@ ending flush background task %u", self, self.taskId);
[[UIApplication sharedApplication] endBackgroundTask:self.taskId];
self.taskId = UIBackgroundTaskInvalid;
}

0 comments on commit d33b5fe

Please sign in to comment.