Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Automatic Lightweight Migration of Core Data on NSPersistentDocument

ALM is great. But I can't get it to work on a project that uses Core Data with NSDocument. It seems that ALM is disabled by default.

Fine. For any normal project, you'd add the two appropriate options to the dictionary in this line:

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error])

...but that line doesn't exist when using CD with NSD (NSPersistentDocument hides the details and I can't see how you can modify them).

The following method seems to offer hope:

configurePersistentStoreCoordinatorForURL:ofType:modelConfiguration:storeOptions:error:

...some people on mailing lists (from 4+ years ago) report success overriding that method, altering the options dictionary, and then re-invoking it on "super". That didn't work for me, sadly. I would advise trying that first if you have similar problems - then come back here if it still doesn't work :)

like image 522
Adam Avatar asked May 16 '11 00:05

Adam


1 Answers

Making a new Cored-Data-with-NSDocument project from scratch, I tried the approach that didn't work originally, and it worked fine this time:

-(BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)url ofType:(NSString *)fileType modelConfiguration:(NSString *)configuration storeOptions:(NSDictionary *)storeOptions error:(NSError **)error
{
    NSMutableDictionary *newOptions = [NSMutableDictionary dictionaryWithDictionary:storeOptions];
    [newOptions setValue:@"YES" forKey:NSMigratePersistentStoresAutomaticallyOption];
    [newOptions setValue:@"TRUE" forKey:NSInferMappingModelAutomaticallyOption];

    return [super configurePersistentStoreCoordinatorForURL:url ofType:fileType modelConfiguration:configuration storeOptions:newOptions error:error];
}
like image 91
Adam Avatar answered Oct 08 '22 22:10

Adam