找不到 momd 文件:核心数据问题 [英] Can't find momd file: Core Data problems

查看:16
本文介绍了找不到 momd 文件:核心数据问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

天啊!我搞砸了!

我是一名 Core Data 菜鸟,正在开发我的第一个 iOS 应用.经过多次堆栈溢出后,我正在使用此代码:

I'm a Core Data noob, working on my first iOS app. After much Stack Overflowing I'm using this code:

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 是我的应用程序的名称.

CoreData is the name of my app.

我试图通过在我的 iPhone 模拟器中找到 sqlite 文件的路径,然后将初始数据放入应用程序,然后插入到该 sqlite 文件中.但是在某些时候,我移动了 sqlite(认为它会创建一个新副本),从模拟器中删除了该应用程序,并且 sqlite 文件消失了.我不确定我是否遗漏了过程的某些部分(这是几个小时前的),但最终的结果是一切都搞砸了.

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.

我如何重新证实这个 sqlite/momd 文件?清除"和清除所有目标"显示为灰色.

How do I resubstantiate this sqlite / momd file? "Clean" and "Clean all targets" are grayed out.

我很高兴从我的应用程序中发布相关代码,这将有助于阐明这个问题,但有大量与核心数据相关的代码我不明白,所以我不确定要发布什么部分!非常感谢任何帮助.

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.

推荐答案

这里有一些建议:

  • 您发布的用于获取 .mom(d) 文件的代码并不是推荐的方式.使用 mergedModelFromBundles 代替,如

  • The code you posted to get the .mom(d) file is not exactly the recommended way. Use mergedModelFromBundles instead, as in

self.managedObjectContent= [NSManagedObjectModel mergedModelFromBundles:nil];

它负责获取路径,选择/合并正确的mommomd,并一步一步地初始化MOC.您应该使用它.但请注意,您需要不时清理构建过程,如 this SO question/回答.

It takes care of getting the path, choosing/merging the correct mom or momd, and initializing of the MOC all by one step. You should use this. But note that you need to clean the build process once in a while, as discussed in this SO question/answer.

这表明,虽然通过 StackOverflow 爬行通常是好的,但是当您处理像 CoreData 这样的大型框架时,这并不是最好的方法.

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 documentation from 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.

此外,Marcus Zarra 的 CoreData 书籍(请参阅此处)对我帮助很大.它可能看起来很贵,但完全值得.

Also, Marcus Zarra's CoreData book (see here) helped me a lot. It might look expensive, but it was totally worth while.

在另一个主题上,我认为将预煮的 sqlite 文件放入模拟器的目录(在 ~/Library/Application Support/iPhone Simulator/内)不是一个好策略>) 即使是出于开发目的...因为它在真实设备上不起作用.如果您需要这样做,请将 sqlite 文件作为应用程序的资源,并在启动时将其复制到 DocumentsCaches,然后再使用它.

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 either Documents or Caches, and use that afterwards.

这篇关于找不到 momd 文件:核心数据问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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