Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data Lightweight Migration: what's required to trigger it?

I've recently successfully done a Core Data "Lightweight Migration"; but I've noticed this required a fair bit of futzing on my part - and I'm wondering what's the absolute minimum necessary to trigger an automatic migration?

I don't mean in terms of "what data changes require a migration or else you'll crash", that's written up in a few places - but rather:

"Here's a list of things you must do at minimum to get Core Data to migrate for you".

( FWIW - below are the things I did. Some seem absolutely necessary, some perhaps not - wondering which are which? ):

  • Editor -> Add Model Version ( named it and saved )
  • Utility Inspector -> Versioned Core Data Model: selected my latest model version as "current"
  • Added my new table to the latest Core Data Model
  • Editor -> Create New NSManagedObject Subclass ...
  • Since I use mogenerator - I used it to create human and machine files (after a bit of configuration to have it pointing to the new model file created above, hiding inside the .xcdatamodeld package), swapped these with the files created in the step above ( mogenerator is a live saver if you're doing Core Data )
  • In my app's info.plist I modified the "Bundle Version String, short" AND "Bundle Version" up an increment.
  • Added the necessary options NSDictionary to my persistentStoreCoordinator initializer method ( [__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:optionsDict error:&error] )

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

I then ran my new app over the previous version, and checked, sure enough, the migration worked and all was peachy ... but that's a lot of steps - however, so far, it DOES look like that's the minimum required.

But, for instance, how important are the options in the info-plist? If I don't change them - or - if I only change one, will the app still migrate correctly?

Thanks

like image 314
OverToasty Avatar asked Dec 06 '25 10:12

OverToasty


1 Answers

  • Editor -> Add Model Version ( named it and saved )
  • Utility Inspector -> Versioned Core Data Model: selected my latest model version as "current"
  • Added the necessary options NSDictionary to my persistentStoreCoordinator initializer method ( [__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:optionsDict error:&error] )

Is all you need.

like image 64
mackross Avatar answered Dec 09 '25 20:12

mackross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!