magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.79k stars 1.79k forks source link

Best practice when truncating during a save? #676

Open seivan opened 10 years ago

seivan commented 10 years ago

I'm having some issues with my NSFetchedResultsController. When the user logs out which fires off a truncate on all entities - that causes an exception in any controller that might currently be in the process of updating it's data.

The exception happens in;

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
  [self.tableView endUpdates];
}
Assertion failure in -[UIViewAnimation initWithView:indexPath:endRect:endAlpha:startFraction:endFraction:curve:animateFromCurrentPosition:shouldDeleteAfterAnimation:editing:], /SourceCache/UIKit/UIKit-2903.23/UITableViewSupport.m:2661

Now I am sure it's my fault in the way I use Magical Record. Right now everything runs with

- (void) MR_saveToPersistentStoreWithCompletion:(MRSaveCompletionHandler)completion;

This happens after end of a network requests that has updated the existing entities.

vascoorey commented 10 years ago

Hi @selvan, the best solution to your problem would probably be to nil out the NSFetchedResultsController before you truncate all tables.

Another way to do this would be to use the suspendAutomaticTrackingOfChangesInManagedObjectContext property on your controller (provided you're using standard boilerplate code) and fire off notifications when you're about to change the core data stack or have just changed it to set the property accordingly.

tonyarnold commented 10 years ago

@seivan — in which context are you performing the truncation? Is it the main/default context, or the root saving context, or a child of either?