Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

NSRunLoop runMode blocks on app start #36

Closed
ViktorasJucikas opened this Issue · 2 comments

2 participants

@ViktorasJucikas

Code snipped below from flushEvents/People in Mixpanel.m seems to be blocking the main thread.

Symptoms: whenever app comes to foreground there's a lag of ~5secs while you can't do anything because UI is blocked. Instruments are showing that below is running then, commenting flushEvents/People out removes this lag.

This is related to a fix to issue 35 (#35) - on app going to background new thread is started and code below tries to keep it alive. The only reason to have a new thread there seems to be this 5sec limit which could run over if you were to base64 encode lots of data (you can actually base64 encode quite a bit of data in 5secs) because NSURLConnection calls are non-blocking.

So maybe the solution is to change it so NSURLConnection is blocking if you think 5secs is not enough to encode data? Or maybe change below in a way so it doesn't block the main thread?

A side bug of the below is that if you're under a very slow connection and close the app and immediately reopen it it crashes - seems to be that this background thread is still kept alive while a new one kicks in for foreground processing but the self.eventsConnection is still alive and they both try to clear it which results in crash.

 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);
   }
@neilrahilly
Owner

Thanks again, Viktoras.

@ViktorasJucikas

Cheers Neil.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.