Poor performance of the Save operation of the master data in the managed entity

I have an app for iPhone / iPad that uses Core Data.

In my database, I have only one table, although it is very large (about 40 columns). When I create a database, I create and insert about 13,000 new entities, and then I call "saveContext".

for (NSArray *singleDiamond in allDiamonds) { @try { if (//Some validation) { Diamond *diamond = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Diamond class]) inManagedObjectContext:self.managedObjectContext]; //Do setup for diamond... } } @catch (NSException *exception) {NSLog(@"%@",[exception message]);} } NSLog(@"Start Saving Context..."); [self saveContext]; NSLog(@"End Saving Context..."); 

The identifier of my problem is that only the saveContext method takes 23 seconds . This is unacceptable.

Is there something I'm doing wrong? How can I improve performance here?

+4
source share
2 answers

You must call saveContext several times during batch insertion, and then call reset to β€œforget” the previous inserted managed objects. For example, in my case, I keep the context every 100 objects. In addition, you must create a dedicated context for the import and optimize it (setting the unsuitable to zero, since you do not need to roll back / cancel the entire insert). Read here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdImporting.html

+4
source

Saving 13,000 items will take some time.

Are 13,000 items saved when you first start the application, so let's not just supply the database as a payload using the application. So when the database does not exist yet, just copy it from the package.

+4
source

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


All Articles