I have a Core Data migration that introduces 2 new entity types. The migration works without issue, but I want to populate the database with default data after the migration.
Currently, my approach is to define a custom NSEntityMigrationPolicy and override endEntityMapping:manager:error:
- (BOOL)endEntityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error {
if (![super endEntityMapping:mapping manager:manager error:error]) return NO;
Theme *defaultTheme = [NSEntityDescription insertNewObjectForEntityForName:@"Theme" inManagedObjectContext:[manager destinationContext]];
[defaultTheme setName:NSLocalizedString(@"Default", @"Default theme name")];
return YES;
}
Yes this is a good approach; probably the best approach currently.
Theme's -awakeFromInsert is not being called because your custom subclasses are not used during migration. The migration manager performs all migration actions with bare NSManagedObject's rather than using any custom objects.
Likewise, you should not declare it as a Theme (the -insertNewObjectForEntityForName: inManagedObjectContext: call is really returning a vanilla NSManagedObject) in that method either. It will just lead to confusion during code maintenance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With