Reputation: 3303
I have an app with some stocks information for example and store it in CoreData. I have two entities Stock(name) and DailyStockData(date,number) and I store two years information for each stock entity. Then user can delete some Stock names in TableView.
In my - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
I delete this Stock and DailyStockData cascade and save context then.
My save code looks like this
// Save the context.
NSError *error = nil;
if (![context save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
The problem is: when User deleting stocks fast - my app crashing. I understand that the reason is "abort" command when my device didn't finish save previous deleting transaction and call to save it again.
So I have two questions here ^) 1. Is there any way to prevent this crash? 2. What is best practice to deal with context save error? May be I can try save it again and again rather than just abort.
The simplest solution is redesign application to save context at the "very" end - for example when my app change view or something but I want to know all possibilities.
Upvotes: 0
Views: 474
Reputation: 64428
- Is there any way to prevent this crash?
Yes, remove the abort()
. The abort is only used in minor example code when you don't want to take the time to log, analyze or respond to errors. You would never use it in real code.
- What is best practice to deal with context save error?
The first thing to do is log the error in detail so that you have some idea what is actually failing.
I understand that the reason is "abort" command when my device didn't finish save previous deleting transaction and call to save it again.
Unlikely. It is almost impossible for UI driven action, which works at human speed, to overload any local computer operation. Saving even several hundred objects in Core Data takes only milliseconds while it takes a human a full second or more to delete a row in the UI. If the crash is happening while the UI is in use, then the error is probably related to the UI e.g. not properly updating the section and row count for the tableview.
Don't guess. Log the save error and find out what is actually happening.
Upvotes: 0
Reputation: 179
What i do is, i create the database using CoreData, but i dont use simulator to put in information into the database. I use Navicat lite to put in information into the database. If you want to delete the whole database, just delete it from the simulators folder under user/library/application support/iphone simulator//applications//documents/. That will delete the whole database, unless u have synched with ur application folder.
Upvotes: 0
Reputation: 5120
You should NOT invoke "save" frequently, it's bad for performance.
The "save" method let the CoreData actually save the modified data to your disk, it takes time if there are a lot of data need to be saved.
Just save once while your App is going to exit or going background is good enough.
If you worry about an unexpected crash will lose some data, you can invoke "save" when your users stop modifying data for a while or exited the edit view in your App. It depend on your decision, but just don't save every time your data is changed.
Upvotes: 3