3

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.

4

2 回答 2

3

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

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

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

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

于 2012-02-07T18:38:53.483 回答
3

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

  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 下修改模型文件。

于 2011-10-21T16:17:38.750 回答