CoreData:创建一个新实体作为旧实体的父实体

CoreData: Create a new entity to be parent of old entities

最近,我正在尝试完善我的代码。我发现我所有的实体都有名为标识符和所有者的属性,所以我想创建一个实体作为它们的父实体,其中包含标识符和所有者。

结果如下,所有对象都继承自名为 SRModel 的父对象,它具有标识符和所有者属性。

但是,在我删除所有这些冗余属性后,持久存储无法自动迁移。 我该如何解决这个问题?我必须自己做迁移吗? 有什么简单的方法吗?

根据 Apple 的 Core Data Model Versioning and Data Migration Programming Guide,您不能自动执行此操作。

You cannot, however, merge entity hierarchies; if two existing entities do not share a common parent in the source, they cannot share a common parent in the destination

请注意 Andy Riordan 关于继承的观点。不要只相信他的话;自己看看新旧模型下生成的.SQLite文件。添加仅具有两个公共属性的父实体只会使您的实体和支持表更大,而没有性能优势。如果真的要注意这两个共同的元素,用一个Protocol把它们叫出来。