当我什至不关心旧数据时,我花费了大量时间将核心数据整理到新的迁移中。有没有一种方法可以删除所有现有数据并跳转到新的数据模型,而不是每次更改数据模型时都处理映射模型的麻烦?
是的,只需删除商店文件并重新创建即可。我经常(至少在开发过程中)让我的代码尝试自动迁移,如果失败,则清除商店并重新开始:
// 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)