0

NSIncrementalStore SubclassのNSManagedObjectModelに新しいエンティティを追加しようとしています。これを loadMetadata メソッドで実行していますが、最後の行でこの例外をスローし続けます。以下のコードを参照してください

「NSInternalInconsistencyException」構成のエンティティは、モデルに既に存在している必要があります

コード

var model:AnyObject=(self.persistentStoreCoordinator?.managedObjectModel.copy())!
var newEntity=NSEntityDescription()
newEntity.name="newEntity"
newEntity.managedObjectClassName="newEntity"

var entities=model.entitiesForConfiguration(self.configurationName)
entities?.append(newEntity)

model.setEntities(entities!, forConfiguration: self.configurationName)
4

2 に答える 2

0

ドキュメントはこれについて明確ではsetEntities:forConfiguration:ありませんが、設定されるエンティティを呼び出す前に、マネージド オブジェクト モデルのentities配列に既に存在している必要があります。これは、このメソッドがモデル内の既存のエンティティを特定の構成に実際に割り当てるためです。

The solution here is to make a mutable copy of the entities array, add your entities to it if they do not exist, and then set the managed object model's entities array to an immutable copy of the modified array. After that point you can invoke setEntities:forConfiguration:.

It would be worth filing a radar bug report on this behavior.

于 2015-10-08T01:21:12.227 に答える
0

モデルが永続ストア コーディネーターに追加された後は、モデルを変更することはできません。モデルを操作できるのは、初期化の直後で、モデルを に適用する前だけNSPersistentStoreCoordinatorです。

于 2015-06-08T00:02:52.673 に答える