【发布时间】:2012-03-21 19:47:39
【问题描述】:
当我什至不关心旧数据时,我花费了大量时间将核心数据按摩到新的迁移中。每次更改数据模型时,不必处理映射模型的麻烦,有没有办法删除所有现有数据并跳转到新数据模型?
【问题讨论】:
标签: objective-c cocoa core-data core-data-migration
当我什至不关心旧数据时,我花费了大量时间将核心数据按摩到新的迁移中。每次更改数据模型时,不必处理映射模型的麻烦,有没有办法删除所有现有数据并跳转到新数据模型?
【问题讨论】:
标签: objective-c cocoa core-data core-data-migration
是的,只需删除存储文件并重新创建它。我经常(至少在开发中)让我的代码尝试自动迁移,如果失败了,就把商店吹走并重新开始:
// storefile is an NSURL to the store file, mom is the NSManagedObjectModel
NSError *err = nil;
NSPersistentStoreCoordinator *psc = [[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom] autorelease];
[psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storefile
options:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],
NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES],
NSInferMappingModelAutomaticallyOption,
nil]
error:&err];
if (err) // could be more specific about testing this error
{ // assume automigration failed, blow away the store and try again
err = nil; // log it first!
[[NSFileManager defaultManager] removeItemAtURL:storefile
error:nil];
[psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storefile
options:nil
error:&err];
}
// then test err again and give up if there's still a problem
【讨论】: