我的核心数据实体在哪里?
我有一个预先存在的项目,我已向其中添加了核心数据模型。我添加了核心数据框架,添加了带有实体的数据模型,并将其与一些生成的 NSManagedObject
类一起包含在我的应用程序的目标中。它编译得很好,现在我想为我创建的实体添加一些测试。按照这些说明,我使用 setUp
方法设置逻辑测试基类,如下所示:
- (void)setUp {
model = [NSManagedObjectModel mergedModelFromBundles:nil];
NSLog(@"model: %@", model);
coord = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
store = [coord addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil
URL:nil
options:nil
error:NULL];
ctx = [[NSManagedObjectContext alloc] init];
[ctx setPersistentStoreCoordinator:coord];
}
编译并创建所有对象。然而,模型没有实体! NSLog()
输出如下所示:
2011-10-29 23:56:58.941 otest[42682:3b03] model: (<NSManagedObjectModel: 0x19c6780>) isEditable 1, entities {
}, fetch request templates {
}
那么我的实体在哪里?我查看了该捆绑包,也没有 .momd
文件。我是否错过了构建模型的一些关键步骤?
I have a pre-existing project to which I've added Core Data models. I added the Core Data framework, added a data model with entities, and included it in my app's target, along with some generated NSManagedObject
classes. It compiles nicely, and now I'd like to add some tests for the entities I've created. Following these instructions, I've set up a logic test base class with a setUp
method like so:
- (void)setUp {
model = [NSManagedObjectModel mergedModelFromBundles:nil];
NSLog(@"model: %@", model);
coord = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
store = [coord addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil
URL:nil
options:nil
error:NULL];
ctx = [[NSManagedObjectContext alloc] init];
[ctx setPersistentStoreCoordinator:coord];
}
This compiles and all the objects are created. However, the model has no entities! The NSLog()
output looks like so:
2011-10-29 23:56:58.941 otest[42682:3b03] model: (<NSManagedObjectModel: 0x19c6780>) isEditable 1, entities {
}, fetch request templates {
}
So where are my entities? I've poked around the bundle, and there are no .momd
files, either. Have I missed some crucial step to get my models to build?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我做了一些额外的 Duck Duck Go 并设法在 这个答案。结果是,因为测试目标不使用“主”包,所以我必须实例化测试包。因此,
我现在有以下三行:
包标识符直接来自我的目标构建信息,而“MyModels”来自我的数据模型文件,该文件名为“MyModels.xcdatamodeld”并包含在应用程序包中,如下所示“MyModels.momd”。当然,其中也包含我的模型。
I did some additional Duck Duck Going and managed to find the information I needed in the this answer. The upshot is that, because a test target doesn't use a "main" bundle, I have to instantiate the test bundle. So instead of this line:
I now have these three lines:
The bundle identifier comes directly from my target build info, while "MyModels" comes from my data model file, which is named "MyModels.xcdatamodeld" and included in the app bundle as "MyModels.momd". And that, of course, contains my models.
看这里。我使用的是当您使用 CoreData 创建项目时生成的代码。我希望这能帮助您解决您的问题:
pragma mark Core Data stack
Look here. I'm using code that is generated when you are creating project with CoreData. I hope that would help you to solve your problems:
pragma mark Core Data stack
如果您的数据库位于框架内/来自框架,则需要使用其相应的 Bundle 加载它。
我在 Swift 中使用
MagicalRecord
找到了一个解决方案:从包中加载管理对象模型并将
setShouldAutoCreateManagedObjectModel
设置为false
就成功了!If your database is within / from a framework, you need to load it using its corresponding Bundle.
I found a solution in Swift with
MagicalRecord
:Loading the managaed object model from the bundle and
setShouldAutoCreateManagedObjectModel
set tofalse
did the trick!