1

I am unable to achieve a simple lightweight migration by simply adding 1 Entity to the datamodel.

I have read and followed all the guides/documentation/posts/answers, I can't seem to find my mistake/error.

  • I do have created a new datamodel from the already existing one.
  • I do have set the new datamodel as current datamodel.
  • I do have only added 1 Entity to the new datamodel (+ link to the parent Entity).
  • I do have passed the dictionary options NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption in the method addPersistentStoreWithType.

I even tried to log everything, thank to the method given from this post: core data migration

/*! The method checks the Core Data file version is compatible with the App's model version
 and then pushes the main menu view onto the navigation stack.  If not compatible it displays a
 message to the user.
 
 @param file The file URL for the Core Data Store. With UIManagedDocument you have to get the
 actual store file URL, you can't just use the UIManagedDocument file URL.
 */
-(void) checkCoreDataFileVersion:(NSURL*)file
{
    if ([self checkVersion:file]) {
        
    // file version is compatible so continue (add code to push the menu view)
        
    } else {
        
        // file version is NOT compatible
        
        _fileOpenErrorAlert = [[UIAlertView alloc] initWithTitle:@"Unable to open Document" message:@"Please check that you have the correct application version installed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [_fileOpenErrorAlert show];
        
    }
    return;
}


/*! Checks the Core Data files models version against the apps model version to see if they
 are compatible.  This will return YES if a lightweight migration can be performed and NO if NOT.
 
 @param fileURL The file URL for the Core Data Store. With UIManagedDocument you have to get the
 actual store file URL, you can't just use the UIManagedDocument file URL.
 @return  Returns YES if they are compatible and NO if not.
 */
- (bool)checkVersion:(NSURL*)fileURL {
    
    NSManagedObjectModel *model = [self managedObjectModel];
      
    NSLog(@" app model entity version hashes are %@", [model entityVersionHashesByName]);
      
    NSError *error;
    NSDictionary *metaData = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:fileURL error:&error];
      
    if (!metaData) {
        NSLog(@"problem getting metaData");
        NSLog(@"  - error is %@, %@", error, error.userInfo);
        return NO;
    }
                      
    bool result = [model isConfiguration:nil compatibleWithStoreMetadata:metaData];
    if (!result) {
        NSLog(@" file is not compatible!");
        NSLog(@" metadata is %@", metaData);
    }
                                      
    return result;
                                      
}

When I make a diff of the metadata from all the Entities, I only match difference for 1 Entity (the newly created). So why it can't make a migration ? I just added 1 Entity.

EDIT :

I don't have Crashes, the App is working fine. There is something I don't understand. When I download our lastest App from the AppStore, launch it and when I build from xCode my lastest developement App (with the new datamodel) over the one from the AppStore, the migration doesn't occur.

BUT when I use GIT, when I put the HEAD to the lastest release TAG, build, launch the App. Then put back the HEAD to my lastest development feature (with the new datamodel etc..), build and run, the migration is done and everything is working.

So which scenario should I trust ?

Community
  • 1
  • 1

1 Answers1

0

Yes, You should trust the 2nd senario to test coredata migration by applying it to the last released code. The first senario is no more valid since Apple for some security reasons nomore give the ability to update an itune-downloaded app using xcode directly.

There was a way to test the upgrade on itune-version but not directly from xcode.

Technical Note TN2285 Testing iOS App Updates

Install an ad hoc distribution of an archived build of the update using iTunes on a device that already has the old version of the app installed.

Installing Your App on Test Devices Using iTunes

Idali
  • 1,023
  • 7
  • 10
  • So why when I use TestFlight the migration is not working if I have the App from the AppStore already installed on the phone ? This is not THE real case scenario of an Update ? it works only with GIT –  Dec 12 '16 at 15:57
  • its not the same, if you have a itune/appStore version installed, to test the update you need to installed the new version using itunes... check the Technical note. – Idali Dec 12 '16 at 16:03
  • Installing Your App on Test Devices Using iTunes check: https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/TestingYouriOSApp/TestingYouriOSApp.html – Idali Dec 12 '16 at 16:07
  • Thank you, I'm trying it, I will accept your answer if it works (i guess it will). –  Dec 12 '16 at 16:13
  • Glad to help :) – Idali Dec 12 '16 at 16:14
  • I archived my application, drag and drop the ipa file with iTunes, installed the App over the one from the Appstore on my device. And... it doesn't work... my the migration is not done. I can't see my new feature. –  Dec 12 '16 at 16:31
  • From the coredata migration it works because else you will have a crash. use Alert to log and check if it pass – Idali Dec 12 '16 at 16:41
  • I'm accepting your answer because apparently this is not a migration issue, else it would have crashed you're right. So this is in the UI maybe –  Dec 12 '16 at 17:03