Versioning

The concept of migrations and versioning is related to the problems that are caused when we make some changes in our data model. Like if we add or delete some attribute(s) from an existing Entity or add one more Entity to our data model. In that case, our new version of application will crash when launched because of the simple fact that the existing data in the persistent store on the iPhone will be unusable in the new version of our application.

This problem can be corrected at our end by selecting Reset Content and Settings option from the iPhone simulator menu or by uninstalling the application from the iPhone and re-installing it. Consequently, the CoreData will create a new persistent store on the basis of the new Data model when the application is launched. But the problem occurs at the client's end, who has the older application, and a new version of the application is sent to them. They cannot uninstall and re-install the application as they will lose their existing data.

To solve such problems, Xcode has a built-in version-control mechanism that helps in applying a new version to our revised data model. In this versioning facility, the older data model will be kept (and not erased) and the revised data model is assigned a new version number. This will not only save the user from losing the existing data but will also help in upgrading the older data into the newer version.

Creating a new Data Model version

Let's assume we have an application with the name demopredicate that has a data model: demopredicate.xcdatamodel. To create a new Data Model version, single click demopredicate.xcdatamodel in Xcode Project window and select Design | Data Model | Add Model Version option. It will add a new version of our data model. We will find that our demopredicate.xcdatamodel file gets disappeared from the Resources group and is replaced by a new resource with the demopredicate.xcdatamodeld with a disclosure triangle next to it, as shown in the following screenshot:

Creating a new Data Model version

On selecting the disclosure triangle of demopredicate.xcdatamodeld, all the versions of our data model will be displayed and the current data model that is being used by our application will be represented by an icon with green checkmark on it. When we create a new version of our data model, the latest version is represented by the file with the original name, and the older version is represented by a copy that is created with the same name with an incrementally larger number affixed to the end. Like in the preceding screenshot, the file: demopredicate2.xcdatamodel is the original data model.

As we have the older version of our data model safe, we can now make changes to the current version (latest version) as desired. We can always switch to the older data model to see our old existing data. To switch to any data model, that is, to make any data model the current version (to work on), select the data model file from the Xcode Project window and select the Design | Data Model | Set Current Version option.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.76.135