title: Executing Core Data saving on a background thread?
body: <p>I've got a button which marks a selected entry in a Core Data SQLite as a "Favourite", meaning I'm just flipping a <code>BOOL</code> for that index from off to on.</p>
  
  <p>Currently, when I do this, I call <code>save</code> on the <code>managedObjectContext</code>, which takes maybe 500ms, perhaps a little more, according to Instruments.</p>
  
  <p>I have some code that executes at the same time which triggers a nifty little particle explosion ("Hooray, a favourite!"), but I'm running into an issue where that explosion is delayed until after the <code>save</code> has completed.</p>
  
  <p>I'm not sure why, since the code to trigger the explosion is before the <code>save</code> call. I'm a relatively new programmer so perhaps I'm missing something, but doesn't code execute line by line in a case like this, in that the explosion would trigger, then the save would occur while it's going? The delegate call here may be taking some time too, but the same question applies, why would it matter if it's after those lines of code?</p>
  
  <p><strong>EDIT:</strong> Am I right in saying that the main thread is being blocked before the particles appear by the next lines of code, meaning the UI can't update itself?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>// Particle animation
  LikeExplosion *likeExplosionView = [[LikeExplosion alloc] initWithFrame: CGRectMake(0, 0, 320, 400)];
  [likeExplosionView setUserInteractionEnabled: NO];
  [self.view addSubview: likeExplosionView];
  [self.view bringSubviewToFront: likeExplosionView];
  [likeExplosionView decayOverTime: 1.1];
  
  // Delegate call to reload tableview elsewhere
  [self.delegate detailViewControllerDidLikeLine];
  
  // Update current object
  [_selectedLine setIsLiked: [NSNumber numberWithBool: YES]];
  [_selectedLine setIsDisliked: [NSNumber numberWithBool: NO]];
  
  // Update context
  NSError *error;
  if (![[[CDManager sharedManager] managedObjectContext] save:&amp;error]) NSLog(@"Saving changes failed: %@, %@", error, [error userInfo]);
  </code></pre>
  
  <p>First question: <strong>why is there a delay, when I'm calling the animation code first in the method?</strong></p>
  
  <p>Second question: <strong>would putting the <code>save</code> call on a background thread solve the problem, and is it safe / a good idea to do so?</strong></p>