Lightweight Migrations in Core Data Tutorial | posavski-obzor.info
CoreData migration is an essential part of Agile approach to app development. Now let's focus specifically on lightweight migration, mapping models, and Add , remove, rename a Relationship; Changing relationship from “to-one” to. Custom migration is quite a powerful tool and you can perform all kinds of transformations, e.g., change relationships or merge entities. Versioned Core Data Model; Lightweight Migrations; Example Scenario; Custom. You can't predict the future, but with Core Data, you can migrate toward the and relates all the objects according to the relationship mapping.
This step will create a second version of the data model, but you still need to tell Xcode to use the new version as the current model. In order to perform any migration, you want to keep the original model file as it is, and make changes to an entirely new model file. In the File Inspector pane on the right, there is a selection menu toward the bottom called Model Version.
Core Data will try to first connect the persistent store with the ticked model version when setting up the stack. The older version is there to support migration.
The current model is the one Core Data will ensure is loaded prior to attaching the rest of the stack for your use.
Migrating a Data Model With Core Data
Make sure you have the v2 data model selected and add an image attribute to the Note entity. Just such a transformer has been provided for you in ImageTransformer.
Next, in the Module field, choose Current Product Module. The new model is now ready for some code! Build and run the app. It turns out lightweight migrations are enabled by default. This means every time you create a new data model version, and it can be auto migrated, it will be.
8 Tips on CoreData Migration
What a time saver! Core Data can automatically look at the differences in two data models and create a mapping model between them. For entities and attributes that are identical between model versions, this is a straightforward data pass through mapping. For other changes, just follow a few simple rules for Core Data to create a mapping model. In the new model, changes must fit an obvious migration pattern, such as: Deleting entities, attributes or relationships Renaming entities, attributes or relationships using the renamingIdentifier Adding a new, optional attribute Adding a new, required attribute with a default value Changing an optional attribute to non-optional and specifying a default value Changing a non-optional attribute to optional Changing the entity hierarchy Adding a new parent entity and moving attributes up or down the hierarchy Changing a relationship from to-one to to-many Changing a relationship from non-ordered to-many to ordered to-many and vice versa Note: As you see from this list, Core Data can detect, and more importantly, automatically react to, a wide variety of common changes between data models.
As a rule of thumb, all migrations, if necessary, should start as lightweight migrations and only move to more complex mappings when the need arises. This means Core Data can easily migrate the old data store to a new one, since this change follows item 3 in the list of lightweight migration patterns. Image attachments Now the data is migrated, you need to update the UI to allow image attachments to new notes. Luckily, most of this work has been done for you. The Create Note scene is attached to a navigation controller with a root view controller relationship.
Control-drag from the navigation controller to the Create Note With Images scene and select the root view controller relationship segue. This will disconnect the old Create Note scene and connect the new, image-powered one instead: Build and run, and choose to add a new note: Tap the Attach Image button to add an image to the note. Thankfully, the iOS Simulator comes with a library of photos ready for your use. The existing code uses the photo album, since there is no camera in the Simulator.
Where to Go From Here? If you enjoyed what you learned in this tutorial, why not check out the complete Core Data by Tutorials bookavailable in our store? This chapter covers the basics of setting up your data model and then adding and fetching records. This chapter will teach you how you customize your own managed object subclasses to store and validate data. Chapter 3, The Core Data Stack: Under the hood, Core Data is made up of many parts working together.
Chapter 4, Intermediate Fetching: Your apps will fetch data all the time, and Core Data offers many options for getting the data to you efficiently. This chapter covers more advanced fetch requests, predicates, sorting and asynchronous fetching.
Table views are at the core of many iOS apps, and Apple wants to make Core Data play nicely with them! Each data model version has a unique identifier and Core Data stores this identifier in the persistent store to know what model was used to create it.
Before we version the data model, we need to revert the data model to its original state. Run the application again. Because we reverted the data model to its original state, the application should no longer crash. It is time to version the data model.
With the data model selected, choose Add Model Version Name the version Lists 2 and base the data model version on Lists. It's the only option available. A small triangle has appeared on the left of the data model in the Project Navigator. Click the triangle to show the list of data model versions.
Migrating a Data Model With Core Data
Note that a green checkmark is added to Lists. This indicates that Lists. If we were to run the application, Core Data would continue to use the original data model version. That's not what we have in mind, though.
Before we make any changes, select Lists. Note that the green checkmark has moved from Lists. Because we haven't run the application yet, we can still modify the new data model version without running into compatibility issues. Don't forget to also add the user relationship to the List entity. Run the application to see if we solved the incompatibility problem we ran into earlier. Are you still running into a crash?
To make changes to the data model, we've added a new data model version. We also marked the new data model version as the active data model version. What we haven't told Core Data is what it should do if it runs into an incompatibility issue. We need to tell it to perform a migration.
Lightweight Migrations in Core Data Tutorial
Migrations I already told you that a persistent store is tied to a particular version of the data model. It keeps a reference to the identifier of the data model. If the data model changes, we need to tell Core Data how to migrate the data of the persistent store to the new data model version.
There are two types of migrations: Lightweight migrations are much easier because Core Data takes care of the heavy lifting for us. To add support for lightweight migrations to the CoreDataManager class, we need to make a minor change. Do you remember that addPersistentStore ofType: To add support for migrations, we pass in a dictionary of options with two keys: If the value of this key is set to true, Core Data attempts to infer the mapping model for the migration based on the data model versions of the data model.
By setting the value of this key to true, we tell Core Data to automatically perform a migration if it detects an incompatibility. What is a mapping model? A mapping model defines how one version of the data model relates to another version.
For lightweight migrations, Core Data can infer the mapping model by inspecting the data model versions. This isn't true for heavyweight migrations and that is what makes heavyweight migrations complex and tedious.
For heavyweight migrations, the developer is responsible for creating the mapping model. With this in mind, we can update the implementation of the do clause of the do-catch statement in the CoreDataManager class.
If you don't run into a crash, then Core Data has successfully migrated the persistent store to the new data model version. Keep It Lightweight Whenever you make a change to a data model, you need to consider the consequences. Lightweight migrations carry little overhead. Heavyweight migrations, however, are a pain.Learn about Relationships with Core Data - Beginning Core Data Tutorial - posavski-obzor.info
Avoid them whenever possible. How do you know if a data model change requires a lightweight or a heavyweight migration? You always need to test the migration to be sure. That said, Core Data is pretty clever and is capable of migrating the persistent store most of the times without your help. Adding or removing entities, attributes, and relationships are no problem for Core Data.
Modifying the names of entities, attributes, and relationship, however, is less trivial for Core Data. If you change the cardinality of a relationship, then you are in for a wild ride. Plan, Plan, Plan Every form of persistence requires planning.
I cannot stress enough how important this phase of a project is.