Updating Core Data objects with fetched data

So far, the only thing you have stored in Core Data is movie names. You will expand this functionality by performing a lookup for a certain movie name through the movie database API. The fetched information will be used to display and store a popularity rating for the movies in the Core Data database.

A task such as this seems straightforward at first; you could come up with a flow such as the one shown in the following steps:

  1. A user indicates their favorite movie.
  2. The movie's popularity rating is fetched.
  3. The movie and its rating are stored in the database.
  4. The interface updates with the new movie.

At first glance, this is a fine strategy; insert the data when you have it. However, it's important to consider that API calls are typically done asynchronously so the user interface stays responsive. More importantly, API calls can be really slow if your user doesn't have a good internet connection. This means that you would be updating the interface with very noticeable lag if the preceding steps are executed one by one.

The following would be a much better approach to implement the feature at hand:

  1. The users indicates their favorite movie.
  2. The users store the movie.
  3. Update the interface with the new movie.
  4. Begin popularity fetching.
  5. Update the movie in the database.
  6. Update the interface with the popularity.

This approach is somewhat more complex, but it will give the user a responsive experience. The interface will respond to new movies immediately by showing them, and then automatically updates as soon as new data is retrieved. Before you can fetch the data and update the models, the Core Data model must be updated in order to store the movie's popularity rating.

Open the Core Data model editor and select the Movie entity. All you have to do is add a new property and name it popularity. Select the Double type for this property because the popularity is stored as a decimal value. You have to make sure that this property is optional since you won't be able to provide a value for it straight away:

If you've worked with Core Data prior to when iOS 10 was released, this is the part where you expect to read about migrations and how you can orchestrate them. However, for simple changes such as this, we don't need to manage migrations. All you need to do is simply build and run your application to regenerate your model definitions, and for a simple change, such as the one we performed just now, Core Data will automatically manage the migration on its own.

If you want to support iOS versions earlier than 10, make sure you read up on Core Data migrations. Whenever you update your models, you have to make sure that your database can properly migrate from one model version to another. During development, this isn't extremely important: you just reinstall the app whenever your models change. However, app updates will crash on launch if the Core Data model isn't compatible with the previous model.

Now that the model is updated, let's figure out how to implement the flow that was described earlier.

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

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