【发布时间】:2025-12-25 12:00:16
【问题描述】:
核心数据模型位于添加到主项目的捆绑包中。现在我必须将核心数据迁移到更新的模型。我一直在关注一些示例like this,所以我想在任何查询或导入发生之前迁移数据库。所以我将代码放在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中,但当时没有加载包含核心数据模型的包。拿到捆绑包说它还没有加载。所以我无法开始迁移过程。
我怎样才能强制它加载这个包?
将核心数据模型放在一个单独的包中而不是主包中是一种好方法吗?
编辑:
- (NSManagedObjectModel*)objectModel
{
if (_objectModel)
return _objectModel;
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:kDataManagerBundleName ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
NSString *modelPath = [bundle pathForResource:kDataManagerModelName ofType:@"momd"];
_objectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];
return _objectModel;
}
对于迁移部分,它表示包尚未加载并为 pathForResource 返回 nil。
【问题讨论】:
标签: ios xcode core-data nsbundle