在NSPersistentDocument上启用核心数据的自动轻量级迁移

Ada*_*dam 4 macos core-data nsdocument

ALM很棒.但我不能让它在使用Core Data with NSDocument的项目上工作.似乎默认情况下禁用ALM.

精细.对于任何正常项目,您可以在此行中为字典添加两个适当的选项:

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error])
Run Code Online (Sandbox Code Playgroud)

...但是当使用带有NSD的CD时,该行不存在(NSPersistentDocument隐藏了详细信息,我看不到如何修改它们).

以下方法似乎提供了希望:

configurePersistentStoreCoordinatorForURL:ofType:modelConfiguration:storeOptions:error:
Run Code Online (Sandbox Code Playgroud)

...邮件列表上的一些人(从4年前开始)报告成功覆盖该方法,更改选项字典,然后在"超级"上重新调用它.遗憾的是,那对我不起作用.如果你有类似的问题,我建议先尝试一下 - 如果它仍然不起作用,那么回到这里:)

Ada*_*dam 7

从头开始创建一个新的Cored-Data-with-NSDocument项目,我尝试了最初不起作用的方法,这次它运行良好:

-(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];
}
Run Code Online (Sandbox Code Playgroud)