Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting or removing ManagedObject in CoreData

Tags:

ios

core-data

In the documentation and in the broad literature the generated factory method to delete/remove a subclassed managed object in CoreData for IOS is

(void)removeXXXObject:(NSManagedObject *)value

where XXX is the corresponding relationship or we can use simply removeObject.

In my code I used this:

Data *lastData = [[self sortedPersonDatas] objectAtIndex:0];
[selectedPerson removePersonDatasObject:lastData];

where PersonDatas is a one-to-many relationship to Data managed object from I took the last data (lastData resulted from a sorted array of all data) But using the first two remove methods and checking the SQL database behind we can find that the actual data is existing just the inverse relationship is null. To completely delete the data (all attributes and the object) I had to use:

[selectedPerson.managedObjectContext deleteObject:lastData];

The question: which is the better method and is it correct that CoreData leaves the data intact?

like image 672
BootMaker Avatar asked Nov 21 '12 13:11

BootMaker


1 Answers

removeXXXObject only removes an object from a to-many relationship, but does not delete the object from the store. To do so, you have to indeed use deleteObject - this is the desired behavior. Calling deleteObject will by default also set the corresponding relationships to nil (see https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html#//apple_ref/doc/uid/TP40001857-SW1).

like image 84
MrMage Avatar answered Sep 29 '22 02:09

MrMage