Skip navigation.
 
mlRe: What exactly happens when I use Core Data's 'Remove' button?
FROM : Chris Hanson
DATE : Fri Aug 11 20:51:34 2006

On Aug 11, 2006, at 6:32 AM, Arthur C. wrote:

> I have a Core Data application where I can add and remove objects 
> from a table. When I use the 'add' button, a method 
> 'awakeFromInsert' is called, which I can use to set up data 
> management such as key-value observing.
>
> Is there a similar method that gets called when the 'remove' button 
> is used? I found that neither the 'dealloc' nor the 
> 'didTurnIntoFault' method gets called. The object-to-be-removed 
> should at least be unregistered as KVO observer. Failing to do this 
> leads to strange behaviour: the removed object reappears in the 
> table view (!) when changing a key this object was observing.
> So another question is why the object is not actually deleted (I 
> would expect it to be dealloc-ed).


To expand a little on what others have said, it's not really correct 
to talk about "Core Data's 'Remove' button".  Core Data is a model-
level technology; your human interface (view) is connected to the 
model via a controller, in this case probably the array controller 
you generated by option-dragging the representation of an entity from 
an Xcode data model to a window in Interface Builder.

When you click the "Remove" button, the button sends -remove: to the 
associated array controller.  The array controller will then perform 
the correct operation on its selection (if any).  What the "correct 
operation" is depends on whether the array controller is managing a 
relationship or all instances of an entity.

If an array controller is managing a relationship — in other words, 
if its contentSet binding is configured — then it will simply remove 
its selected objects from that relationship.  You can override this 
behavior by setting the Deletes Objects on Remove build setting, 
which will cause the array controller to first remove its selected 
objects from the relationship and then ask its managed object context 
to delete them.

If an array controller is managing all instances of an entity, when 
it receives -remove: it will ask its managed object context to delete 
the selected objects.  And when a managed object context is asked to 
delete an object, that doesn't actually dispose of the object 
immediately.  It marks the object as deleted and will actually remove 
the object when the context is saved.

You can think of this as sort of like a database transaction:  If you 
have a database and start a transaction, then ask the database to 
delete some rows, they aren't actually deleted in a way that's 
visible to any other users until the transaction is committed.  If 
the transaction is rolled back instead of committed, the deletion is 
actually undone.

  -- Chris