如何编辑从 iOS5 中的 momd 加载的 NSManagedObjectModel

发布于 2024-12-11 05:04:31 字数 845 浏览 2 评论 0原文

对于使用 CoreData 的 iOS 应用程序,我需要一个模型,其中某些实体属性在应用程序的 iPhone 和 iPad 版本之间有所不同。 为了实现这一点,我使用 initWithContentsOfURL: 从应用程序包中的 momd 文件加载 NSManagedObjectModel。但是,在 storeCoordinator 实际使用模型之前,我以编程方式修改模型中的一些实体(基于我存储在设备特定 plist 中的信息)。 这曾经在 iOS4 上完美运行。正如苹果公司在其文档中所述,

托管对象模型在被对象图管理器(托管对象上下文或持久存储协调器)使用之前是可编辑的。

在iOS5中似乎不再是这种情况了(尽管文档仍然声明了这一点)。一旦创建模型,使用 initWithContentsOfURL: 例如,任何修改尝试都会抛出“NSInternalInconsistencyException”,原因:“无法修改不可变模型。”如果我在创建模型对象后立即在调试器中打印模型对象的描述,它会读取“isEditable 0”,而在 iOS4 上运行相同的代码时,它会读取“isEditable 1”。

按照 Apple 的建议,在模型上使用“copy”创建可编辑副本,还会返回带有“isEditable 0”的模型。

我对此有两种可能的解释:

  • Bug。我在 openradar 上找不到匹配的错误报告,如有必要,我会提交一份。
  • iCloud 集成。我还不太熟悉 iCloud API,但我知道可以设置与 CoreData 的某种集成。我想也许某些自动协调器可以在创建模型时访问我的模型,使其不可编辑。

我将继续深入研究这些选项,但如果有人有这方面的经验,我将不胜感激。

For my iOS application using CoreData, I need to have a model where certain entities properties vary between the iPhone and iPad version of the app.
To achieve this, I load a NSManagedObjectModel from a momd file located in my application bundle, using initWithContentsOfURL: . But, before the model is actually used by the storeCoordinator, I modify some entities programmatically in the model (based on information I store in a device specific plist).
This used to work flawlessly on iOS4. As Apple states in its documentation,

Managed object models are editable until they are used by an object graph manager (a managed object context or a persistent store coordinator).

This does not seem to be the case in iOS5 anymore (although the documentation still states this). As soon as the model is created, with initWithContentsOfURL: for example, any attempt to modify throws an 'NSInternalInconsistencyException', reason: 'Can't modify an immutable model.' If I print the description of the model object in the debugger just after it was created, it reads 'isEditable 0', where the same reads 'isEditable 1' when running the same code on iOS4.

Using "copy" on the model the create an editable copy, as suggested by Apple, also returns a model with "isEditable 0".

I see two potential explanations for this :

  • Bug . I couldn't find a matching bugreport on openradar, I will file one if necessary.
  • iCloud integration. I'm not very familiar with iCloud APIs yet, but I know some kind of integration with CoreData can be set up. I imagine that maybe some automatic coordinator could access my model at the time it's created, rendering it uneditable.

I'll continue digging into these options, but if anybody has experience with this, it would be greatly appreciated.

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

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

发布评论

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

评论(2

一身软味 2024-12-18 05:04:31

这是一个错误或未记录的更改。我运行了这个测试代码:

  NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TestManageObjectModelEdits" withExtension:@"momd"];
  NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
  NSEntityDescription *e=[[NSEntityDescription alloc] init];
  [e setName:@"PEntity"];
  NSArray *a=[mom entities];
  NSArray *b=[a arrayByAddingObject:e];
  [mom setEntities:b];
  NSLog(@"mom = %@",mom);

…在 iOS 4.3 和 5.0 下。它在 4.3 下工作,并抛出错误,指出它无法在 5.0 下修改模型文件。

It's a bug or an undocumented change. I ran this test code:

  NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TestManageObjectModelEdits" withExtension:@"momd"];
  NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
  NSEntityDescription *e=[[NSEntityDescription alloc] init];
  [e setName:@"PEntity"];
  NSArray *a=[mom entities];
  NSArray *b=[a arrayByAddingObject:e];
  [mom setEntities:b];
  NSLog(@"mom = %@",mom);

… under iOS 4.3 and 5.0. It works under 4.3 and throws an error saying it can't modify the model file under 5.0.

っ左 2024-12-18 05:04:31

我于 10 月 5 日就这个问题提交了雷达,今天收到了以下回复:

您可以通过在 NSManagedObjectModel 实例上调用 -mutableCopy 来获取模型的可变副本。

这适用于 iOS 5 模拟器,我尚未在设备上进行测试。我自己没有尝试,因为 NSManagedObjectModel 不符合 NSMutableCopying (根据文档),并且头文件没有提到 -(id)mutableCopy。

早在 10 月份,我就通过将 momd 文件中的模型与空的 NSManagedObjectModel 合并来创建一个新的 NSManagedObjectModel 来解决这个问题。我认为 momd 文件中的模型现在在内部是不可变的,但其他模型(来自复制或合并)实际上是可变的。

I filed a radar on this very issue on Oct 5 and received the following response today:

You can get a mutable copy of the model by calling -mutableCopy on the NSManagedObjectModel instance.

This worked on the iOS 5 simulator, I haven't yet tested on a device. Didn't try that myself because NSManagedObjectModel doesn't conform to NSMutableCopying (according to the documentation), and the header file doesn't mention -(id)mutableCopy.

I had worked around it back in October by creating a fresh NSManagedObjectModel by merging the one from the momd file with an empty NSManagedObjectModel. I suppose the models from momd files now are internally immutable, but the others (from copy or merge) are actually mutable.

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