使用Core Data跳过痛苦的迁移并移至新的数据模型 [英] Skipping painful migration with Core Data and move to the new data model

查看:129
本文介绍了使用Core Data跳过痛苦的迁移并移至新的数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我甚至不关心旧数据时,我花了很多时间将核心数据压入新的迁移中。而不是每次我改变我的数据模型处理映射模型的麻烦,有没有办法只是删除所有现有的数据和跳转到新的数据模型?

I'm spending a lot of time massaging core data into a new migration when I don't even care about the old data. Instead of dealing with the hassle of mapping models each time I change my data model, is there a way to just delete all existing data and jump to the new data model?

推荐答案

是的,只要删除商店文件并重新创建即可。我经常(至少在开发中)有我的代码尝试自动迁移,如果失败,吹走商店,重新开始:

Yep, just delete the store file and recreate it. I often (at least in development) have my code try an auto migration, and if that fails, blow away the store and start over:

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

这篇关于使用Core Data跳过痛苦的迁移并移至新的数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆