Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling errors in addPersistentStoreWithType

I am trying to find information on handling errors when creating a persistent store coordinator on the iPhone. I have implemented lightweight migration

   NSError *error = nil;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&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. 

     Typical reasons for an error here include:
     * The persistent store is not accessible;
     * The schema for the persistent store is incompatible with current managed object model.
     Check the error message to determine what the actual problem was.


     If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

     If you encounter schema incompatibility errors during development, you can reduce their frequency by:
     * Simply deleting the existing store:
     [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

     * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
     @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

     Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}    

return _persistentStoreCoordinator;

This is based on the code from Apple with the added support for lightweight migration.

I can't find any information on handling errors if the application would still encounter an error here. It seems to me that if the database cannot be created the application can't be used at all.

  • Do I just ask the user to try reinstalling the application and display relevant infromation ?
  • Can I keep the abort() statement while adding a prompt about the error or will this cause the application to be rejected by Apple ?
like image 899
Hugo Tunius Avatar asked Dec 10 '12 18:12

Hugo Tunius


1 Answers

Calling abort() in this situation is out of question. Any app that crashes will be rejected by Apple. And it does not solve the problem: Starting the app again will find the same store file and therefore fail again.

For the same reason, reinstalling the app does not help, and it would be a bad user experience.

Of course, the situation should not occur if the migration has been tested. But if this fatal error occurs and your app cannot open the database, you have to create a fresh database.

The exact steps to take depend on what is stored in the database and if/how you can recover the data. So you could

  • remove the old database file with [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil], or copy a default database file from your programs resources to storeURL,
  • call _persistentStoreCoordinator addPersistentStoreWithType:... again to open the new database.
  • perhaps fill the database again with data from a server, or whatever has to be done to recreate the data.
like image 108
Martin R Avatar answered Oct 20 '22 22:10

Martin R