title: Prevent dispatch_after() background task from being executed
body: <p>This is my issue. When my application enters background I want it to perform a function after certain period of time. This is what I do:</p>
  
  <pre><code>- (void)applicationDidEnterBackground:(UIApplication *)application
  {
      isRunningInBackground = YES;
  
      taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
  
      int64_t delayInSeconds = 30;
      dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
      dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
      {
          [self doSomething];
      });
  }
  
  - (void)doSomething
  {
     NSLog(@"HELLO");
  }
  </code></pre>
  
  <p><code>taskIdentifier</code> variable is declared in myAppDelegate.h file like this:</p>
  
  <pre><code>UIBackgroundTaskIdentifier taskIdentifier;
  </code></pre>
  
  <p>Everything works as it supposed to, I see that console prints HELLO just right after 30 seconds are gone. But I don't want <code>doSomething</code> to be executed if the app enters foreground until 30 seconds are over. So I need to cancel it. This is how i do that:</p>
  
  <pre><code>- (void)applicationWillEnterForeground:(UIApplication *)application
  {    
      isRunningInBackground = NO;
      [self stopBackgroundExecution];
  }
  
  - (void)stopBackgroundExecution
  {
      [[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];
      taskIdentifier = UIBackgroundTaskInvalid;
  }
  </code></pre>
  
  <p>But unfortunately it doesn't cancel <code>doSomething</code>, it is still performed. What am I doing wrong? How do I cancel that function? </p>