ios 找不到 momd 文件:核心数据问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4536414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't find momd file: Core Data problems
提问by thekevinscott
Aw geez! I screwed something up!
天啊!我搞砸了!
I'm a Core Data noob, working on my first iOS app. After much Stack Overflowing I'm using this code:
我是一名 Core Data 菜鸟,正在开发我的第一个 iOS 应用程序。经过大量堆栈溢出后,我正在使用此代码:
NSString *path = [[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"momd"];
if (!path) {
path = [[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"mom"];
}
NSAssert(path != nil, @"Unable to find Resource in main bundle");
CoreData is the name of my app.
CoreData 是我的应用程序的名称。
I've tried to put in initial data into the app by finding the path to the sqlite file in my iPhone simulator, and then going and inserting into that sqlite file. But at some point, I moved the sqlite (thinking it would create a fresh copy), deleted the app from the simulator, and the sqlite file is gone. I'm not sure if I'm leaving out some part of the process (this was a few hours ago) but the end result is that everything is screwed up.
我试图通过在我的 iPhone 模拟器中找到 sqlite 文件的路径,然后将初始数据放入应用程序,然后插入到该 sqlite 文件中。但是在某些时候,我移动了 sqlite(认为它会创建一个新副本),从模拟器中删除了该应用程序,并且 sqlite 文件消失了。我不确定我是否遗漏了过程的某些部分(这是几个小时前),但最终的结果是一切都搞砸了。
How do I resubstantiate this sqlite / momd file? "Clean" and "Clean all targets" are grayed out.
我如何重新证实这个 sqlite / momd 文件?“清除”和“清除所有目标”显示为灰色。
I'm happy to post the relevant code from my app that would help shed some light on this problem but there's tons of code relating to Core Data which I don't understand, so I'm not sure what part to post! Any help is greatly appreciated.
我很高兴从我的应用程序中发布相关代码,这将有助于阐明这个问题,但有大量与核心数据相关的代码我不明白,所以我不确定要发布什么部分!任何帮助是极大的赞赏。
回答by Yuji
Here are a few recommendations:
以下是一些建议:
The code you posted to get the
.mom(d)
file is not exactly the recommended way. UsemergedModelFromBundles
instead, as inself.managedObjectContent= [NSManagedObjectModel mergedModelFromBundles:nil];
It takes care of getting the path, choosing/merging the correct
mom
ormomd
, and initializing of the MOC all by one step. You shoulduse this. But note that you need to clean the build process once in a while, as discussed in this SO question/answer.This shows that, although crawling through StackOverflow is often good, that's not the best approach when you deal with a big framework like CoreData.
Honestly, take a day and read the documentationfrom the start to the end. You might want to google bits of code and to start coding immediately, but reading through the documentation definitely saves the development time considerably in the long run.
Also, Marcus Zarra's CoreData book (see here) helped me a lot. It might look expensive, but it was totally worth while.
On a different topic, I don't think it a good strategy to put the pre-cooked sqlite file into the simulator's directory (inside
~/Library/Application Support/iPhone Simulator/
) even for development purpose... because it doesn't work on the real device. If you need to do so, put the sqlite file as a resource of the app, and copy it at the launch time to eitherDocuments
orCaches
, and use that afterwards.
您发布的用于获取
.mom(d)
文件的代码并不完全是推荐的方式。使用mergedModelFromBundles
替代,如self.managedObjectContent= [NSManagedObjectModel mergedModelFromBundles:nil];
它负责一步一步地获取路径、选择/合并正确的
mom
ormomd
和初始化 MOC。你应该使用这个。但请注意,您需要不时清理构建过程,如本 SO question/answer 中所述。这表明,虽然通过 StackOverflow 爬行通常是好的,但当您处理像 CoreData 这样的大框架时,这不是最好的方法。
老实说,花一天时间从头到尾阅读文档。您可能想搜索一些代码并立即开始编码,但从长远来看,通读文档肯定会大大节省开发时间。
此外,Marcus Zarra 的 CoreData 书籍(请参阅此处)对我帮助很大。它可能看起来很贵,但它完全值得。
在另一个主题上,我认为
~/Library/Application Support/iPhone Simulator/
即使出于开发目的,将预煮的 sqlite 文件放入模拟器的目录(inside )也不是一个好策略……因为它在真实设备上不起作用。如果您需要这样做,请将 sqlite 文件作为应用程序的资源,并在启动时将其复制到Documents
或Caches
,然后再使用它。
回答by Craig
If the clean targets menu items are grey, it could be that you have the app running.
如果干净的目标菜单项是灰色的,则可能是您正在运行应用程序。
The way you're getting the path to the mom/momd looks like you've tried to integrate CoreData after the fact (rather than opting to have it included when you created the project). I had the same issues as you probably do. The default code that looks for momd assumes it exists, and you should be able to, as well, if you add a version of your data model to the project.
您获得妈妈/妈妈路径的方式看起来就像您事后尝试集成 CoreData(而不是选择在创建项目时包含它)。我遇到了和你一样的问题。查找 momd 的默认代码假定它存在,如果您将数据模型的一个版本添加到项目中,您也应该能够这样做。
To do that, select the xcdatamodel file, and in the Design menu select Data Model > Add Model Version. Then, you'll have CoreData.xcdatamodeld with a subtree containing Coredata.xcdatamodel and a new version. You can just delete the extra version and you'll have the hierarchy you need. If you build and look in the App bundle, you'll see the CoreData.momd directory with the mom inside.
为此,请选择 xcdatamodel 文件,然后在“设计”菜单中选择“数据模型”>“添加模型版本”。然后,您将使用包含 Coredata.xcdatamodel 和新版本的子树对 CoreData.xcdatamodeld 进行建模。您可以删除额外的版本,您将拥有所需的层次结构。如果您构建并查看 App 包,您将看到 CoreData.momd 目录,其中包含 mom。
Those steps aren't included in any of the CoreData tutorials I've found so far. I hope it's helpful!
到目前为止,我发现的任何 CoreData 教程中都不包含这些步骤。我希望它有帮助!
回答by Steve Goodman
Practical answer: You might try the following code if you're having path issues. You'll also want to check that your xcdatamodel file is included as a resource for your target.
实用答案:如果遇到路径问题,可以尝试以下代码。您还需要检查您的 xcdatamodel 文件是否作为目标的资源包含在内。
[[NSBundle bundleForClass:[self class]] pathForResource:@"CoreData" ofType:@"momd"]
Better answer: There's a distinction between a momd file, which represents your NSManagedObjectModel, and a sqlite file, which is used by your NSPersistentStore object. The best approach to working with CoreData is to let the SDK handle all the interaction with sqlite. The only thing you should need to set up regarding sqlite is telling your NSPersistantStore where to stash your sqlite file. The managed objects that you define in your managed object model will handle all the data insertion and updates for you.
更好的答案:代表您的 NSManagedObjectModel 的 momd 文件和您的 NSPersistentStore 对象使用的 sqlite 文件之间存在区别。使用 CoreData 的最佳方法是让 SDK 处理与 sqlite 的所有交互。关于 sqlite,你唯一需要设置的就是告诉你的 NSPersistantStore 在哪里存放你的 sqlite 文件。您在托管对象模型中定义的托管对象将为您处理所有数据插入和更新。
If you're not familiar with the objects I've referenced, check out the apple docs for a very helpful overview. You'll definitely want to take the time to grasp the full CoreData stack, as the time investment will pay off in far less headaches if you work with Core Data the way Apple expects. It is a more abstracted design than many web developers are accustomed to, but trying to circumvent that abstraction by working directly with SQLite creates its own set of complexity.
如果您不熟悉我引用的对象,请查看苹果文档以获得非常有用的概述。您肯定希望花时间掌握完整的 CoreData 堆栈,因为如果您按照 Apple 期望的方式使用 Core Data,那么时间投资的回报将大大减少。它是一种比许多 Web 开发人员习惯的更抽象的设计,但试图通过直接使用 SQLite 来绕过这种抽象会产生其自身的复杂性。
回答by Fiid
I found it very valuable to go look at my executable in the simulator "disk image".
我发现在模拟器“磁盘映像”中查看我的可执行文件非常有价值。
If you head into ~/Library/Application Support/iPhone Simulator//Applications, you can see the apps on the phone. Figure out which one is yours (use find, or look at the date), and then browse into your target.app and look for the .momd file.
如果您进入 ~/Library/Application Support/iPhone Simulator//Applications,您可以看到手机上的应用程序。找出哪个是你的(使用 find 或查看日期),然后浏览到你的 target.app 并查找 .momd 文件。
In my case, my momd name was different from my .xcdatamodeld file name, which was why core data couldn't load it. Once I corrected the name in the AppDelegate things started working correctly.
就我而言,我的 momd 名称与我的 .xcdatamodeld 文件名不同,这就是核心数据无法加载它的原因。一旦我更正了 AppDelegate 中的名称,事情就开始正常工作了。
Note that the momd "file" seems to actually be a directory (like the .app setup).
请注意,momd“文件”似乎实际上是一个目录(如 .app 设置)。
--
——
In terms of adding the code to initialise core data; I did this too. I created a new blank project with core data enabled, and copied over all the initialisation steps for core data in the AppDelegate file. There's a whole bunch of stuff related to initialising the whole stack, so if that is not present, you need to put it there.
在添加初始化核心数据的代码方面;我也这样做了。我创建了一个启用核心数据的新空白项目,并复制了 AppDelegate 文件中核心数据的所有初始化步骤。有一大堆与初始化整个堆栈相关的东西,所以如果不存在,你需要把它放在那里。
回答by quantumpotato
If you added a version to your model, change .mom to .momd
如果您向模型添加了版本,请将 .mom 更改为 .momd
NSString *path = [[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"momd"];
if (!path) {
path = [[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"momd"];