Updating Core Data objects with fetched data

So far, the only thing we have stored in Core Data is movie names. We will expand this functionality by performing a lookup for a certain movie name through the movie database API. We will use the fetched information to display and store a popularity rating for the movies in our 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. The users fill out their favorite movie.
  2. It fetches popularity.
  3. It stores the movie and its popularity.
  4. The interface updates with the new movie.

At first sight, 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 interface stays responsive. Also, 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 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 fill out their favorite movie.
  2. The users store the movie.
  3. Update the interface with the new movie.

 

  1. Begin popularity fetching.
  2. Update the movie in the database.
  3. Update the interface with popularity.

This approach is somewhat more complex, but it will give the user a very snappy and responsive experience. The interface will respond to new movies immediately, and the interface automatically updates as soon as new data is retrieved. Before we can fetch the data and update our models, we will update our Core Data model in order to store the movie 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 before 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 like 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 that 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, we can figure out how to implement the flow that was described earlier.