RonLugge
RonLugge

Reputation: 5174

Why do I need to remove relationships before deleting an object in core data?

I have a program in which I'm using CoreData to manage both permanent and temporary data.

In one instance, I abort the addition of some data by deleting the object when the user presses cancel. But when I hit the next view, the data is still there, attached to it's parent core data object.

    [self.bar removeFoosObject:self.foo];//Why do I need this line?
    [self.foo.managedObjectContext deleteObject:self.foo];

I eventually solved this by manually removing the child object from it's parent -- but isn't that something core data handles automatically? Why should I need the first line?

I ran some test code, and Foo is definitely deleted -- the code that it was mucking up let me check, and it's MOC has been set to nil. The memory exists, but it should be very, very dead...

Upvotes: 1

Views: 1727

Answers (1)

Pochi
Pochi

Reputation: 13459

You have to manually do this because you have your deletion rules set wrong. Check the Relationship Delete Rules in the following apple documentation.

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html

Or it could also be that the relationship is NOT set as optional between the parent and child object.

And also after you delete the object you should save the database to synchronize it.

Upvotes: 1

Related Questions