简体   繁体   中英

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

Recently, I am trying to refine my code. I found that all my entities has attributes named identifier and owner, so I want to create a entity to be their parent which contains identifier and owner.

在此输入图像描述

Following is the result, all object inherit from a parent named SRModel, which has identifier and owner attributes.

在此输入图像描述

However, after I delete all these redundant properties, the persistent store is not able to auto migration. How can I solve the problem? Do I have to do migration by myself? Are there any simple way to do so?

According to Apple's Core Data Model Versioning and Data Migration Programming Guide , you can't do that automatically.

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

Note Andy Riordan's point about inheritance. And don't just take his word for it; look at the generated .SQLite files yourself under the old and new models. Adding a parent entity with only two common attributes will just make your entities, and backing tables, larger, with no performance benefit. If you really want to note the two common elements, use a Protocol to call them out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM