CoreDataで、デフォルトのオートマイグレーションを使うのではなく、自前で2つの NSPersistentContainer
で loadPersistentStores()
して、一方から読み込み、もう一方に書き込むということをしました。
このとき書き込み側で、
CoreData: warning: Multiple NSEntityDescriptions claim ....
あるいは
CoreData: error: +[XXXX entity] Failed to find a unique match for an NSEntityDescription ...
のような警告もしくはエラーが発生しました。
やっていたこと。
let object = Xxxx(context: moc)
...attrbuteに値を代入...
do {
try moc.save()
} catch {
print("Failed to save new note")
}
これを下記のようにすることで回避できました。
let discription = NSEntityDescription.entity(forEntityName: "Xxxx", in: moc)!
let object = Xxxx(entity: discription, insertInto: moc)
moc.insert(object)
...attrbuteに値を代入...
do {
try moc.save()
} catch {
print("Failed to save new note")
}
moc
は NSPersistentContainer
のviewContext: NSManagedObjectContext
です。
参考記事は以下です。
stackoverflow: Multiple NSEntityDescriptions Claim NSManagedObject Subclass