gpt4 book ai didi

objective-c - 跳过痛苦的 Core Data 迁移并迁移到新的数据模型

转载 作者:行者123 更新时间:2023-12-03 16:27:15 25 4
gpt4 key购买 nike

当我什至不关心旧数据时,我花费了大量时间将核心数据整理到新的迁移中。有没有一种方法可以删除所有现有数据并跳转到新的数据模型,而不是每次更改数据模型时都处理映射模型的麻烦?

最佳答案

是的,只需删除存储文件并重新创建它即可。我经常(至少在开发过程中)让我的代码尝试自动迁移,如果失败,则清除商店并重新开始:

// 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

关于objective-c - 跳过痛苦的 Core Data 迁移并迁移到新的数据模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9548010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com