Error handling in addPersistentStoreWithType

I am trying to find error handling information when creating a persistent storage coordinator on an iPhone. I implemented easy 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 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 Apple code with added support for lightweight migration.

I cannot find any error handling information if the application still encounters an error here. It seems to me that if the database cannot be created, the application cannot be used at all.

  • Will I just ask the user to try reinstalling the application and display the relevant information?
  • Can I save the abort () statement when adding an error request or will this lead to a rejection of the Apple application?
+6
source share
1 answer

Calling abort () in this situation is out of the question. Apple may refuse any application that crashes. And this does not solve the problem: starting the application will again find the same storage file and, therefore, will work again.

For the same reason, reinstalling the application does not help, and it will be a bad user interface.

Of course, the situation should not arise if migration is tested. But if this fatal error occurs and your application cannot open the database, you need to create a new 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

  • delete the old database file using [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] or copy the default database file from your program resources to storeURL ,
  • call _persistentStoreCoordinator addPersistentStoreWithType:... again to open a new database.
  • perhaps filling the database again with data from the server or something else that needs to be done to recreate the data.
+14
source

All Articles