title: UI-updates with Background tasks on iOS
body: <p>I have a synchronisation process which sometimes (even rarely) takes 20 seconds. I used to call the selector in the background. There is a progress indicator which stops animating on a notification that the sync is finished, and with the code below it changes straight away: </p>
  
  <pre><code>[self performSelectorInBackground:@selector(updateFunction) withObject:nil];
  </code></pre>
  
  <p>But in order to allow the progress to continue I decided to use the background task:</p>
  
  <pre><code>    UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
  
      __block UIBackgroundTaskIdentifier background_task; //Create a task object
  
      background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
          [application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
          background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
  
          //System will be shutting down the app at any point in time now
      }];
  
      //Background tasks require you to use asyncrous tasks
  
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
          //Perform your tasks that your application requires
  
          NSLog(@"\n\nRunning in the background!\n\n");
          [self updateFunction];
  
          [application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
          background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
          NSLog(@"Finished in the background!");
      });
  </code></pre>
  
  <p>But now, when the task is finished the notifications is sent (and executed) but the cell in the table doesn't update until (sometimes) a significant amount of time. So it seems to me that the 'needs display' is not triggered when coming from a background task or so, and is only updated at a regular refresh interval or so.</p>
  
  <p>Is there a way to have the table cell update using background tasks, or am I missing something?</p>
  
  <p>Edit:
  The notification listener is:</p>
  
  <pre><code>[[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(refreshBttn)
                                               name:@"finishedUpdate"
                                             object:nil]; 
  </code></pre>
  
  <p>The code is:</p>
  
  <pre><code>- (void) refreshBttn {
      NSLog(@"Refresh Buttun action");
      iDomsAppDelegate *delegate = (iDomsAppDelegate *)[[UIApplication sharedApplication] delegate];
      if([delegate updating]){
          [_updateBttn setHidden:TRUE];
          [_activityIndicator startAnimating];         
      } else {
          [_updateBttn setHidden:FALSE];
          [_activityIndicator stopAnimating];        
      }
      [self setNeedsDisplay];
      [self setNeedsLayout];
  }
  </code></pre>