Error transferring master data with an error: Failed to save the new store after the first migration pass

In the past, I have already successfully implemented automatic migration from version 1 of my data model to version 2. Now, using SDK 3.1.3, the transition from version 2 to version 3 ended with the following error:

Unresolved error Error Domain = NSCocoaErrorDomain Code = 134110 UserInfo = 0x5363360 "Operation could not be completed. (Cocoa error 134110.)", {NSUnderlyingError = Error Domain = NSCocoaErrorDomain Code = 256 UserInfo = 0x53622b0 "Operation could not be completed. 256.) "; reason = "Failed to save the new store after the first migration."; }

I tried automatic migration using NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption , as well as migration using only NSMigratePersistentStoresAutomaticallyOption , providing a mapping model from v2 to v3.

I see that the specified error is registered and there is no object in the application. However, if I exit the application and open it again, everything will be in place and will work.

The main data methods I use are as follows:

 - (NSManagedObjectModel *)managedObjectModel { if (managedObjectModel != nil) { return managedObjectModel; } NSString *path = [[NSBundle mainBundle] pathForResource:@"MYAPP" ofType:@"momd"]; NSURL *momURL = [NSURL fileURLWithPath:path]; managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL]; return managedObjectModel; } - (NSManagedObjectContext *) managedObjectContext { if (managedObjectContext != nil) { return managedObjectContext; } NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; if (coordinator != nil) { managedObjectContext = [[NSManagedObjectContext alloc] init]; [managedObjectContext setPersistentStoreCoordinator: coordinator]; } return managedObjectContext; } - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (persistentStoreCoordinator != nil) { return persistentStoreCoordinator; } NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"MYAPP.sqlite"]]; NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; NSError *error = nil; persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) { // Handle error NSLog(@"Unresolved error %@, %@", error, [error userInfo]); } return persistentStoreCoordinator; } 

In the simulator, I see that this generates the MYAPP ~ .sqlite files and the MYAPP.sqlite file. I tried to delete the MYAPP ~ .sqlite file, but

 BOOL oldExists = [[NSFileManager defaultManager] fileExistsAtPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"MYAPP~.sqlite"]]; 

always returns NO. Any clue? Am I doing something wrong? Thank you in advance.

+4
source share
2 answers

I ran into this, and after reading as many Apple documents and web messages as I could find, there was no answer. In my case, manual migration also worked, but when I went to open a new coordinator, it would give the same error as yours. Finally, I decided to go back to my last working version of the data model and make a series of small changes / versions and see where it broke the possibilities of automatic migration in order to expand it further, and it turned out that this was not so. Now I can add entities, attributes and relationships without problems, and they are automatically transferred. Have you accidentally deleted the intermediate version of datamodel?

+1
source

For what it's worth, the Core Magical Record overhead package includes this hack:

 [coordinator MR_addAutoMigratingSqliteStoreNamed:storeFileName]; //HACK: lame solution to fix automigration error "Migration failed after first pass" if ([[coordinator persistentStores] count] == 0) { [coordinator performSelector:@selector(MR_addAutoMigratingSqliteStoreNamed:) withObject:storeFileName afterDelay:0.5]; } 

You can try something like this. I could not find an explanation of what the problem was, though, or why just repeating this would work.

+1
source

Source: https://habr.com/ru/post/1308030/


All Articles