The main issue of iOS data migration

When I started developing the application, I had a Core data model with 5 entities (with the name Visitors, UnreadMessages, ContactStatuses, UserVCardand User). The app appeared live on the app store. There is no problem. Now I had to add some changes to the requirements, so the main data model changed slightly, adding 2 more objects to it (with the name AudioSupportedWindowsand AudioMessages)

Thus, according to many articles, SO posts, and documentation for Apple, the migration method was LightWeight migration , so I added the following code for this

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,    
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

NSError *error;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType        configuration:nil URL:storeUrl options:options error:&error]) {
    // Handle error
    NSLog(@"Problem with PersistentStoreCoordinator: %@",error);
}

in my method persistentStoreCoordinator. But he always gives an error . Can't find model for source store

, - ()

, , , , 5 . The model used to open the store is incompatible with the one used to create the store

em ... .

1

- (NSManagedObjectModel *)managedObjectModel
{
    if (managedObjectModel != nil)
    {
        return managedObjectModel;
    }

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"UserData" withExtension:@"momd"];
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

    return managedObjectModel;
}
+3
4

... . , xcdatamodeld, App Store, , .. - , ...

em , :)

0

, . , ( ), , .

" ". , . , , .

+1

, , , , . , iTC:

  • "/", , , " Finder"
  • : VersionInfo.plist
  • plist,

- - ( ), , .

+1

, . , . xcode , , :

// Model.momd/Model is the path of the previous model. Make sure it correct
NSURL *oldUrl = [[NSBundle mainBundle] URLForResource:[NSString stringWithFormat:@"Model.momd/Model"] withExtension:@"mom"];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:oldUrl];
NSLog(@"Old model version hashes: %@", model.entityVersionHashesByName);

// Change model with your model name
NSURL *currentModelUrl = [[NSBundle mainBundle] URLForResource:[NSString stringWithFormat:@"Model"] withExtension:@"momd"];
NSManagedObjectModel *currentModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:currentModelUrl];
NSLog(@"Current model version hashes: %@", currentModel.entityVersionHashesByName);



// storeUrl is the path to the persistent store, created by the app store version
NSDictionary *metadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:storeUrl error:nil];
NSLog(@"Store metadata: %@", metadata);

. , , , , , . , (, ), .

+1
source

All Articles