使用 mergedModelFromBundles:和版本控制 (CoreData)

发布于 2024-08-22 02:31:07 字数 948 浏览 4 评论 0原文

我正在尝试使用 CoreData 中的迁移功能。我遵循了苹果文档。我在以下方法中遇到问题:

/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
 */
- (NSManagedObjectModel *)managedObjectModel {

    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    /* 
     * NSInvalidArgumentException', reason: '*** -[NSCFArray insertObject:atIndex:]: attempt to insert nil'
     * 2010-02-17 16:27:15.338 Patrimoine[3037:207]
     */ 
    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    
    return managedObjectModel;
}

http://iphonedevelopment.blogspot.com/2009/09/core-data-migration-problems.html

但我确实选择了苹果建议的方法,即使用菜单选项“添加模型版本”。

你有什么想法吗?

I'm trying to use the migration feature in CoreData. I've followed the Apple Documentation. I have a problem in the following method:

/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
 */
- (NSManagedObjectModel *)managedObjectModel {

    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    /* 
     * NSInvalidArgumentException', reason: '*** -[NSCFArray insertObject:atIndex:]: attempt to insert nil'
     * 2010-02-17 16:27:15.338 Patrimoine[3037:207]
     */ 
    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    
    return managedObjectModel;
}

It appears that there is the same problem on http://iphonedevelopment.blogspot.com/2009/09/core-data-migration-problems.html

Yet I did choose the method Apple suggests, by using the menu option "Add Model Version".

Do you have any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

无需解释 2024-08-29 02:31:08

对我来说不幸的是,这不是清理或版本控制的问题。我有一个有点复杂的项目,有多层嵌套的 Xcode 项目。为了处理每个项目位于不同 SVN 存储库中的问题,我将项目的路径设置为相对于我在 Xcode“首选项 --> 位置”中设置的某些特定源树。例如,我将 FACEBOOK_IOS_SDK_SRC 设置为指向“~/Documents/{my_crazy_folder_struct}/facebook-ios-sdk/src”。这样,每当我的同事检查我的项目时,Facebook SDK 的位置就与他们设置的任何路径相关,并且不必在他们的计算机上与我的计算机上具有完全相同的结构。我知道有更好的方法来处理这个问题,但目前它正在发挥作用。

无论如何 - 我的问题出现了,因为在某些时候我的 *.xcdatamodel 文件位置为“相对于 {source_tree_path}”,它发现该文件很好,甚至将其编译成 *.mom 文件,但我变得如此疯狂最初问题中列出的错误。我将位置更改为“相对于组”并且它开始工作。我还没有找到确切的原因,但它浪费了一天的大部分时间进行调查。希望这能帮助其他人在未来。

Unfortunately for me, it wasn't an issue of cleaning or versioning. I have a somewhat complicated project with several layers of nested Xcode projects. To handle the issue of each project being in different SVN repository, I set up the paths to my projects as relative to some specific source trees that I set up in the Xcode "Preferences --> Locations". For instance, I set up FACEBOOK_IOS_SDK_SRC to point to "~/Documents/{my_crazy_folder_structure}/facebook-ios-sdk/src". This way, whenever my colleagues checkout my project, the location of the Facebook SDK is relative to whatever path they set up and doesn't have to be in the exact same structure on their machines as it is on mine. I know there are better ways to handle this but it is working for now.

Anyways - my issue came up because at some point I had my *.xcdatamodel file location as "Relative to {source_tree_path}", which found the file just fine, and even compiled it into a *.mom file but I was getting this crazy error listed in the initial question. I changed my location to "Relative to Group" and it started working. I haven't found the exact reason for this yet but it wasted the better part of a day investigating. Hopefully this will help someone else out in the future.

枫林﹌晚霞¤ 2024-08-29 02:31:08

我也开始遇到 [[NSManagedObjectModel mergedModelFromBundles:nil] 崩溃,但没有错误消息(典型的核心数据)。我最终解决了这个问题,因为我意识到我在不同模型中用相同的名称命名了两个实体。这就是为什么它对我来说是崩溃的。更改名称可以阻止它崩溃。

I started getting crashes for [[NSManagedObjectModel mergedModelFromBundles:nil] as well, with no error message (typical core data). I eventually solved it by realising that I had named two entities in different models with the same name. That is why it was crashing for me. Changing the names stopped it crashing.

饮惑 2024-08-29 02:31:07

您需要清理您的项目。一旦您对模型进行“版本控制”,Xcode 就会将其移动到捆绑包(文件夹)中,但不会删除旧的。然后发生的情况是,下次运行应用程序时,捆绑包中会出现模型的两个副本;旧的和新的都在 momd 包里。

做一个项目->全部清理将解决此问题。

You need to clean your project. As soon as you "version" your model Xcode moves it into a bundle (folder) but it does not remove the old one. What happens then is that the next time you run your app there are two copies of your model in the bundle; the old one and the new one that is inside of the momd bundle.

Doing a Project -> Clean All will resolve this issue.

客…行舟 2024-08-29 02:31:07

另外,如果您在任何时候重命名模型,请务必通过切换到较旧的模型然后再次返回到较新的模型来重复“设置当前模型”步骤。我的构建设置不会自动重置,并不断将“当前模型名称”设置为不存在的模型,导致完全相同的问题。

您始终可以在构建产品的资源文件夹、导入的 .momd 目录内、名为 versioninfo.plist 的文件中验证此设置是否正确 - 当前模型的设置必须与模型的实际名称匹配。

Also, if you rename your models at any point, be sure and repeat the "set current model" step by switching to an older then back to the newer model again. My build setting would not reset itself automatically and kept setting the "current model name" to a nonexistent model, resulting in the exact same problem.

You can always verify this setting is correct in the build product's resource folder, inside the imported .momd directory, in a file called versioninfo.plist - the setting for the current model MUST match the actual name of your model.

哭了丶谁疼 2024-08-29 02:31:07

我发现使用 mergedModelFromBundel: 方法似乎不适用于迁移;我切换到 -initWithContentsOfURL:,它工作正常。请注意,您必须使用指向“.momd”文件的 URL 来初始化它。

I've found that using the mergedModelFromBundel: method doesn't seem to work with migration; I switched to -initWithContentsOfURL:, and it works fine. Note that you have to init it with a URL pointing to a ".momd" file.

笔芯 2024-08-29 02:31:07

如果您在任何时候更改数据模型,请务必在模拟器中重置内容和设置,否则您将收到错误消息,用于创建商店的模型版本与用于访问它的模型版本不同。

If you change your data model at any point, be sure to Reset Content and Settings in the simulator, or you will get the error that the version of the model used to make the store is not the same as the model used to access it.

左秋 2024-08-29 02:31:07

谢谢大家!

这些都有效,但只有在我使用“干净构建”选项之后才有效。

有关更多信息,请参阅另一个 Stack Overflow 主题:
使用 mergedModelFromBundles: 和版本控制 (CoreData)

iPhone 核心数据参考有相当不错的写法对此。文档中的正式流程是“自动轻量级迁移”。

再次感谢,
——巴特加尔

Thanks to everyone!

These all worked, but only after I used the Clean build option.

See this other Stack Overflow topic for more info:
Using mergedModelFromBundles: and versioning (CoreData)

The iPhone Core Data Reference has a fairly decent write up on this. The formal process from the documentation is an "Automatic Lightweight Migration".

Thanks again,
--Batgar

累赘 2024-08-29 02:31:07

起初,上述方法都不适合我。
我正在使用 XCode 4。
执行干净、干净的构建文件夹,找到 .app 文件以检查除了正确的文件(包含旧版本和新版本)之外是否没有其他 .xcdatamodel 文件。
尝试使用 initWithContentsOfURL:。
验证当前版本是否对应正确的版本,并在模拟器上重置内容和设置。
然后编辑VersionInfo.plist,发现NSManagedObjectModel_CurrentVersionName值对应的是一个不存在的版本名称!

因此,重置某些现有模型的当前版本名称(然后您会看到绿色复选标记发生变化),重新构建并运行。

供参考!

At first, none of the mentioned methods worked for me.
I'm using XCode 4.
Performed Clean, Clean Build Folder, located the .app file to check there's no other .xcdatamodel file there except the correct one (containing both old and new versions).
Tried to use initWithContentsOfURL:.
Verified the current version corresponds to the correct one and did Reset Content and Settings on the simulator.
Then edited the VersionInfo.plist and discovered that the NSManagedObjectModel_CurrentVersionName value corresponds to a non-existing version name!

So reset the current version name for some existing model (you then see the green checkmark changing), rebuilt and it worked.

FYI!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文