NSFetchedResultsController member is triggered even if no context persists

I have an NSManagedObject object that overrides validateForInsert and validateForUpdate.

These methods return NO correctly when something is wrong in the consistency of the objects, based on some logic I wrote.

The app is a classic uitableview maintained by an NSFetchedResultsController with a detailed view controller.

When I add a new object, a detail view controller is instantiated with an object identifier nil and pushed onto the navigation stack. When I pull out the detail controller, [context save: & error] is called, and after putting some breakpoint I found that the selected delegate method is triggered once when I add a new object, even if validateForInsert returns NO and includePendingChanges is set as well NO.

When I retry pushing the verbose controller again, the save will of course be called again, also the validator method, but this time the NSFetchedResultsControllerDelegate will not be triggered.

I would like to know if this is normal behavior or if something is missing in my model.

[UPDATE]

This stack trace after the breakpoint in the delegate methods:

#0  0x0003e5ba in -[MyTableViewController controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:] at ......
#1  0x011512f9 in -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] ()
#2  0x00b46a29 in __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_0 ()
#3  0x01759855 in ___CFXNotificationPost_block_invoke_0 ()
#4  0x01759778 in _CFXNotificationPost ()
#5  0x00a8b19a in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#6  0x0106a673 in -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] ()
#7  0x01101f5e in -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] ()
#8  0x01065ad3 in -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] ()
#9  0x0106916b in -[NSManagedObjectContext save:] ()
#10 0x00004490 in -[MyAppDelegate saveContext:]

      

You can see that the validate is not called even if the save is called in the context, and therefore this will cause the selected controllers' delegate to be fired.

[UPDATE 2]

In particular, don't know if it's related, this throws an exception (not always) in the controllerDidChangeObject:

CoreData Error: Serious Application Error. Ruled out. from the NSFetchedResultsController delegate during the call to -controllerDidChangeContent :. Invalid update: Invalid number of rows in section 0. The number of rows contained in an existing section after update (4) must be equal to the number of rows contained in this section before update (3), plus or minus the number of rows inserted or deleted from this section ( inserted 0, 0 deleted) and plus or minus the number of rows moved to or from this section (0 moved to, 0 dropped). with userInfo (null)

and infact in origin table has three rows, just because the context is bypassed by the validateFor methods, when the object is instantiated in the context, it seems like the row is inserted, there is a mismatch in the number of rows.

The controller delegate is pretty straightforward and standard:

-(void)controllerWillChangeContent:(NSFetchedResultsController*)controller {
   [self.tableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
   // for testing purpose I am not doing any modification
   return;
}

-(void)controllerDidChangeContent:(NSFetchedResultsController*)controller {
   [self.tableView endUpdates];
}

      

However, I don't know who is updating the table view

+1


source to share


1 answer


I think this is normal behavior; the recipient of the results does not need to be saved before it shows the changes. If you do not want changes made in your detail view to appear in the main file before they are saved, you will need to create a separate NSManagedObject context for the detail view. This way, only objects saved to persistent storage will fall back to the selected result controller.



+7


source







All Articles