CoreData Context Conservation Performance

I ended up converting my application to use the CoreData layer for the small data store that I want to use. I have some performance issues and how best to use it. In particular: I have many runs where I read from disk attributes in files: each attribute should generate a new object, if only an object of this type and this value already exists. So, for each file I read: select to check if this managed entity exists; if so, then I create an object, assign a value and save the context.

Currently, I save the context once every time I create a new object, so it happens more or less ten times (for ten attributes) for every file I read (which can be hundreds). Would it be better to reduce the persistence points of the context, perhaps once per file instead of a single attribute? I don’t know the overhead for this operation, so I don’t know whether it’s normal to do this so often or how to find out the time spent on it (maybe with tools? I don’t know how to do it).

+7
iphone core-data
source share
2 answers

There is no need to save after setting each attribute.

Usually you save only the managed object when the code is executed with it, and saving cancels the undo. In the setup you describe, you can safely generate hundreds of managed objects before storing them in persistent storage. You can have a large number (thousand) of light (text attributes) objects in memory without stressing the iPhone.

The only problem on the iPhone is that you never know when the application will be paused or turned off. This saves more than other platforms. However, not to the extent that you are currently using.

The Master Data Effectiveness section of the guide helps you plan. Tools allow you to see the details of Core Data performance.

However, I would not do anything until you tested the application with a lot of data and found it slow. Premature optimization is the source of all evil. Do not waste time to prevent a problem that you may not have.

+11
source share

To prevent an β€œabrupt application stop”, you can implement something like this method:

- (void)saveContext { NSError *error = nil; NSManagedObjectContext *managedObjectContext = self.managedObjectContext; if (managedObjectContext != nil) { if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { /* Replace this implementation with code to handle the error appropriately. abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. */ LogError(@"Unresolved error %@, %@", error, [error userInfo]); // abort(); } } 

}

and use it in two methods of the application delegate:

 - (void)applicationWillTerminate:(UIApplication *)application; 

and

 - (void)applicationDidEnterBackground:(UIApplication *)application; 

Thought it might not be a 100% solution, but in most cases it will work ...

+3
source share

All Articles