I searched within SO but I didn't find a any suggestions to boost performances on deleting Managed Object in Core Data when dealing with relationships.
The scenario is quite simple.
As you can see there are three different entities. Each entity is linked in cascade with the next. For example, FirstLevel
has a relationship called secondLevels
to SecondLevel
. The delete rule from FirstLevel
to SecondLevel
is Cascade while the delete rule from SecondLevel
to FirstLevel
is Nullify. The same rules are applied between SecondLevel
and ThirdLevel
.
When I want to delete the entire graph, I perform a method like the following:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"FirstLevel" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *items = [context executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];
// delete roots object and let Core Data to do the rest...
for (NSManagedObject *managedObject in items) {
[context deleteObject:managedObject];
}
Taking advantage of the Cascade rule the graph is removed. This works fast for few objects but decreases performances for many objects. In addition, I think (but I'm not very sure) that this type of deletion performs a lot of round trips to the disk, Am I wrong?
So, my question is the following: how is it possible to remove the graph without taking advantage of Cascade rule and boost performances but, at the same time, maintain graph consistency?
Thank you in advance.
EDIT
I cannot delete the entire file since I have other entities in my model.
EDIT 2
The code I posted is wrapped in the main
method of a NSOperation
subclass. This solution allows the deleting phase to be excuted in background. Since I took advantage of Cascade Rule the deletion is performed in a semi automatic way. I only delete root objects, the FirstLevel
items, by means of the for loop within the posted code. In this way Core Data to do the rest for me. What I'm wondering is the following: is it possible to bybass that semi-automatic delete operation and do it manually without losing the graph consistency?