Core data migration. Fatal error. The database at <database path> is corrupted. The database disk image is malformed

Hello Apple,
I am using coredata in my current application. But getting error in migration process for rare users - "The database disk image is malformed". Have you any solution to handle this error without deleting database? Because delete or replace database is not good solution for user. I tired with and without journal_mode but same error I got in NSLog.

Below code I used for migration.


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }   
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"];
    NSError *error = nil;
   
    NSMutableDictionary *pragmaOptions = [NSMutableDictionary dictionary];
    [pragmaOptions setObject:@"DELETE" forKey:@"journal_mode"];
   
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, pragmaOptions, NSSQLitePragmasOption, nil];
   
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
       // abort();
    }
   
    return _persistentStoreCoordinator;
}

Unfortunately, I have seen this as well. For me the problem was that my app was loading the store in the foreground and, depending on the size of the catalog, the app could get killed while migrating. The worst part is this can result in a corrupted file. I had read somewhere that the migration was performed on a copy of the file so that if something went wrong during the migration the original file was left intact. Clearly this was not the case, or there is still a window of vulnerability.

Core data migration. Fatal error. The database at &lt;database path&gt; is corrupted. The database disk image is malformed
 
 
Q