Re: Core Date migration


Jim
 

On Jul 22, 2019, at 8:56 AM, Steve Mills via Groups.Io <sjmills@...> wrote:

In my Core Data model, I've added a 2nd version with a new attribute. This attribute needs to be initialized to a sequential value upon migration from the old version. So I'm using a mapping model and a custom migration policy to set this new attribute's value on every entity object.

But, it's not sticking. Do I need to be doing something else in my performCustomValidationForEntityMapping:manager:error: method to cause the changes I've made to be saved? Or am I just interpreting the incredibly vague documentation for this method wrong?

-(BOOL) performCustomValidationForEntityMapping:(NSEntityMapping*)mapping manager:(NSMigrationManager*)manager error:(NSError* _Nullable __autoreleasing*)error
{
BOOL result = YES;

if([manager isKindOfClass:[DocumentMigrationManager class]]) {
Document* doc = ((DocumentMigrationManager*)manager).doc;
NSFetchRequest* fetchRequest = [NSFetchRequest new];
NSEntityDescription* assetEntDesc = [Asset entityInManagedObjectContext:manager.destinationContext];

fetchRequest.entity = assetEntDesc;

NSArray<Asset*>* asses = [manager.destinationContext executeFetchRequest:fetchRequest error:error];

for(Asset* ass in asses) {
// ass.order = @(doc.nextAssetOrder);
[ass setValue:@(doc.nextAssetOrder) forKey:AssetAttributes.order];
}
}

return result;
}
Do you call save on the context somewhere? I’ve not needed to use a mapping like this yet but it seems like if you’re changing attributes you’d want to save.

Jim Crate

Join {cocoa@apple-dev.groups.io to automatically receive all group messages.