title: Calling -[NSRunLoop runMode:beforeDate:] on main thread causes queued NSOperations to run on main thread
body: <p>I ran into an issue today where my app was failing to start in time. It turns out some third-party code I was using was trying to get the user agent string using the trick below:</p>
  
  <pre><code>-(NSString*)userAgentString
  {
      webView = [[UIWebView alloc] init];
      webView.delegate = self;
      [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"www.google.com"]]];
  
      // Wait for the web view to load our bogus request and give us the secret user agent.
      while (self.userAgent == nil) 
      {
          // This executes another run loop. 
          [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
      }
  
      return self.userAgent;
  }
  
  -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
  {
      self.userAgent = [request valueForHTTPHeaderField:@"User-Agent"];
  
      // Return no, we don't care about executing an actual request.
      return NO;
  }
  </code></pre>
  
  <p>(from <a href="http://blog.sallarp.com/iphone-ipad-get-user-agent-for-uiwebview/#more-1003" rel="nofollow">http://blog.sallarp.com/iphone-ipad-get-user-agent-for-uiwebview/#more-1003</a>)</p>
  
  <p>Prior to calling that code, I had queued some operations by adding them to the queue returned by +[NSOperationQueue mainQueue]. These operations are intended to be executed in the background as they make calls to +[NSData dataWithContentsOfURL:].</p>
  
  <p>Once the method on the run loop is called, it executes my queued operation, blocking the main thread and preventing the app from launching. Right now I've gotten a workaround by making sure to not queue any operations until after the third-party code executes, but if anyone knows why this is happening, and how to prevent it in the future, I'd love to hear it. Thanks!</p>