title: dispatching to main queue always fails
body: <p>I'm trying to dispatch some code to the main queue via GCD on iOS but even the most simple tests always fail. In the end It boiled down to this:</p>
  
  <pre><code>static const int TICK_INTERVAL = 1;
  
  #pragma UIApplicationDelegate implementation
  
  - (void) doTick
  {
      if (![NSThread isMainThread])
      {
          NSLog(@"Trying to dispatch . . .");
          dispatch_sync(dispatch_get_main_queue(), ^{
              NSLog(@"test . . .");
          });
      }
  }
  
  - (void) startUpdate
  {
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  
          timer_ = [NSTimer
                    scheduledTimerWithTimeInterval:TICK_INTERVAL
                    target:self
                    selector:@selector(backgroundUpdate)
                    userInfo:nil
                    repeats:NO
                    ];
  
          [[NSRunLoop currentRunLoop]
           addTimer:timer_
           forMode:NSRunLoopCommonModes
           ];
  
          [[NSRunLoop currentRunLoop] run];
      });
  
      UIBackgroundTaskIdentifier back =
      [[UIApplication sharedApplication]
       beginBackgroundTaskWithExpirationHandler:^{
           [self doTick];
           [[UIApplication sharedApplication]
            endBackgroundTask:back
            ];
       }];
  }
  
  -(void)backgroundUpdate
  {
      [self doTick];
  
      UIBackgroundTaskIdentifier back =
      [[UIApplication sharedApplication]
       beginBackgroundTaskWithExpirationHandler:^{
           [self doTick];
           [[UIApplication sharedApplication]
            endBackgroundTask:back
            ];
       }];
  
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
              timer_ = [NSTimer
                       scheduledTimerWithTimeInterval:TICK_INTERVAL
                       target:self
                       selector:@selector(backgroundUpdate)
                       userInfo:nil
                       repeats:NO
                       ];
  
              [[NSRunLoop currentRunLoop]
               addTimer:timer_
               forMode:NSRunLoopCommonModes
               ];
  
              [[NSRunLoop currentRunLoop] run];
          });
  }
  
  - (id) init
  {   
      self = [super init];
      [self startUpdate];
      return self;
  }
  </code></pre>
  
  <p>That's the my <code>AppDelegate</code>.
  I would expect the NSLog to be executed in the main thread to log the test text above, but nothing happens. <code>dispatch_sync</code> code just waits forever and the breakpoint I placed inside the block is never reached.</p>
  
  <p>I made sure that the code isn't executed in the main thread. Before testing with <code>dispatch_sync</code>, I experimented with <code>dispatch_async</code> in my app, the results being, of course, basically the same: nothing happens (without the blocking).</p>
  
  <p>Interestingly enough, it just doesn't seem to work with the main queue, other queues (current queue, global queue) appear to work just fine.</p>
  
  <p>I'm using Phonegap (Cordova) in my app, if that's of any significance.</p>
  
  <p><em>Any</em> ideas?</p>
  
  <p>Thanks a lot!</p>