Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data Crash - The NSPersistentStoreCoordinator has no persistent stores (corrupt file)

I am seeing a few crash reports with this title:

The NSPersistentStoreCoordinator has no persistent stores (corrupt file). It cannot perform a save operation. My code for adding the persistent store to the coordinator is here:

    NSURL *applicationDocumentsDirectory = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].lastObject;

    NSURL *storeURL = [applicationDocumentsDirectory URLByAppendingPathComponent:@"myDatabase.sqlite"];

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"myDataModel" withExtension:@"momd"];

    NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

    NSDictionary *storeOptions = @{NSInferMappingModelAutomaticallyOption:@YES,
                                   NSMigratePersistentStoresAutomaticallyOption:@YES,
                                   NSSQLitePragmasOption:@{@"synchronous": @"OFF"}};

    // Create the persistent store.
    self.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
    NSError *error = nil;
    if (![self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                       configuration:nil
                                                                 URL:storeURL
                                                             options:storeOptions
                                                               error:&error])
   {

    }

I have logged the error that comes out of the 'addPersistentStoreWithType' call, and it is the following:

NSCocoaErrorDomain 259
NSSQLiteErrorDomain = 11;
NSUnderlyingException = "Fatal error. The database at <path> is corrupted. SQLite Error code:11, 'database disk image is malformed'

I assume this means the core data base is damanged on unrecoverable. Is that the case? I have a backup plan of destorying the core database and repopulating from my server, but I would like to know if its definitely unrecoverable, and if there is anyting I can do to figure out why it may have happened in the first place.

Some additional information:

The crash reports note RAM Free 3-6%, Disk Free 57%, newer model phones running iOS 10. The version of the app that started seeing these reports also was the first version that had a lightweight core data migration as part of it, I'm not sure if that makes a difference. I do know that 98% of users successfully upgraded to that version and did the migration with no problem. Here is the stack trace from the report. Despite what it says about "device_locked", I know this crash happens anytime I try to save to the persistent store, even when the user is in the app.

enter image description here

like image 817
haplo1384 Avatar asked Oct 30 '22 11:10

haplo1384


1 Answers

  1. See if you can get acceptable performance without the synchronous pragma. If it isn't helping you a lot, then no need to take that chance.

  2. I see you already have code that can effectively detect when things have gone belly up...when you hit that, you could see what happens by simply deleting the SHM and WAL files. It's possible that it's just those that are messed up.

  3. If you are indeed able to recover from server data, and it really is a rare occurrence, then maybe it IS acceptable to just do as you said and delete the entire database (if #2 doesn't help).

  4. Make sure you are creating the store on the main thread and also make sure the migration is taking place on the main thread.

like image 81
ghostatron Avatar answered Nov 15 '22 05:11

ghostatron