简体   繁体   中英

What is the criteria of compatibility between a store metadata and a managed object model?

My app uses CoreData framework and I want to check compatibility between a store metadata and a managed object model. I do it standard way:

BOOL isModelCompatible = [model isConfiguration:nil
                    compatibleWithStoreMetadata:metadata];

and it returns NO . However the entities in the metadata are the same as in the model. The same number of entities and each entity has the same name. However the model indeed has changed since the time the store was created using this model, I removed a couple of attributes in one entity. And I'm wondering if that is enough for a model to become incompatible with the store metadata. I took a look into the official documentation and it says:

This method compares the version information in the store metadata with the entity versions of a given configuration

And the problem (as it often happens when I read Apple's docs) is that I'm not quite sure what exactly this phrase means. So can anyone explain more regarding that topic? How CoreData decides if a model is compatible or not to a metadata given that enteties in the metadata are the same as in the model?

A model is incompatible with a persistent store any time there's a difference that affects how data is stored in the data file. Removing attributes would qualify, since that change would affect how data was saved in SQLite. Some changes don't lead to incompatibility-- for example, if you changed a relationship from optional to required-- because the data file would be the same either way.

If you want to get the exact details, look at the versionHash property of NSEntityDescription and related classes. That will tell you exactly what's used, and anything not mentioned doesn't affect compatibility. For example on NSEntityDescription it includes

The values which affect persistence are: the name of the entity, the version hash of the superentity (if present), if the entity is abstract, and all of the version hashes for the properties.

To continue from there, look up the same property on NSPropertyDescription and its subclasses.

Core Data's model migration gets past the incompatibility by updating the persistent store to match the new data model. Often this can be done automatically, as with lightweight migration . Removing attributes would allow lightweight migration.

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