Performance of deleteObject master data and persistence of managed object context

I am trying to find a better way to mass delete objects inside my Core Data database.

I have some objects with a parent / child relationship. Sometimes I need to “update” the parent object by clearing all existing child objects and adding new ones to Core Data. The “delete all” part of this operation is where I ran into the problem. I accomplish this by looping through the children and calling deleteObject for each of them.

I noticed that after calling NSManagedObjectContext: Save following all deleteObject calls, it is very slow when I delete 15,000 objects.

How to speed up this challenge? Are there things during the save operation that I can know about and avoid by setting different parameters or tuning my model in a different way? I noticed that bursts of memory during this operation as well. I just want to "remove * from".

Thank.

+3
source share
3 answers

Suppose you have a parent and a child in the Core Data model, and the parent has many relationships for the child children, you should be able to delete all children without cycling as follows:

NSManagedObject *parentObject = ...;
[parentObject setValue:nil forKey:@"children"];

or using the method generated by the basic information

- (void)removeChildren:(NSSet *)value;

NSSet *children = [parentObject valueForKey:@"children"];
[parentObject removeChildren:children];

, NSManagedObjectContext. , .

+1

: , , . , .

+1

- apple: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdCreateMOs.html :

. deleteObject:, , , .

[aContext deleteObject:aManagedObject];

This removes the managed entity from the entity graph. Just as a new object is not stored in the repository until the context is saved, the deleted object will not be deleted from the repository until the context is saved.

+1
source

Source: https://habr.com/ru/post/1744023/


All Articles