Search code examples
ioscore-data

Core-data add new non optional attribute to model with light weight migration


I want to add a new non optional field to current model and upon migration set the value to already existing field.

Models:

ModelV1 {
   timestamp
}

ModelV2 {
  timestamp, creationTime
}

Upon migration I want to set the creationTime to timestamp as it is a non optional field. I don't want to provide a default value as it will be constant.

What I did:

  • Create new version of model.
  • Created mapping file v1 -> v2
  • In mapping file set creationTime -> source.timestamp

But when I run the app I get this error:

Cannot migrate store in-place: Validation error missing attribute values on mandatory destination attribute

The creationTime should not be empty as I have already provided the migration.


Solution

  • The issue was mapping model was not found by the app, i checked the installed app bundle and indeed the mapping file was not present. It was happening because the core-data migration mapping file was present in MyLibrary module but in the final framework it was not present.

    I used cocapods to build the MyLibrary.framework, which needs to be updated to include the mapping file. I added this and it worked.

    s.resource_bundles        = {'myLib' => ['myLib/Database/*.xcmappingmodel']}