问题
I'm trying to create a mapping model for my app that has some custom code. Thus I created a xcmappingmodel and I subclassed NSEntityMigrationPolicy and implemented the
createDestinationInstancesForSourceInstance
method. Further I entered the classname in the Custom Policy field in my mapping model. Unfortunately my code will never be called (tried breakpoints and logs). My store is created with the following options:
NSMigratePersistentStoresAutomaticallyOption: YES
NSInferMappingModelAutomaticallyOption: YES
I'm using XCode 4.0.2 (and tested with XCode 4.2 too). All other of my "lightweight" migrations are running smoothly. What do I need to do to get my custom migration running?
回答1:
How does your model versions differ? Core Data has to recognize that a store is not compatible with the model (represented as a NSManagedObjectModel object). This is done by creating hash values of the properties of each entity and hashing these values to represent a hash value for each entity. If those values do not change the models cannot be differentiated.
There is more information needed about the change in the data model to help you.
回答2:
NSInferMappingModelAutomaticallyOption should be set to NO.
Also, if the model isn't different, you can set the Version Hash Modifier in the Entity inspector. Then Core Data will recognize the model as being different:
https://developer.apple.com/library/mac/documentation/cocoa/conceptual/CoreDataVersioning/Articles/vmUnderstandingVersions.html
来源:https://stackoverflow.com/questions/6442041/custom-policy-in-my-xcmappingmodel-will-not-be-executed