Introduction

This is the third part of the series CoreData: CRUD With Concurrency In Swift: UPDATE.

If you didn’t read the first part, I would suggest you to read it since I introduced this series. You can find the second part here.

In this article we are going to learn how to update the data with CoreData using background queues—to avoid blocking the main queue.

CoreData provides mainly two ways to do it: Either using a NSManagedObject or using NSBatchUpdateRequest.

Happy Reading!

Contents

Using NSManagedObject

Let’s consider that in our app we want to allow the user to add some dogs in a list “Favorites”. We would need to add a new boolean attribute isFavorite in the Dog entity:

At this point, when an user wants to add the dog in the “Favorites” list, we have to set the property isFavorite to true in an object Dog.

Let’s see how to update a Dog and save the changes:

iOS 8+

iOS 10+

When we update a NSManagedObject object, then we must save these changes manually—like in the examples above—to keep the persistence of these changes.

Using NSBatchUpdateRequest

As we said in “Using NSManagedObject, our App allows the user to add a dog in the list “Favorites”.

At this point, we want to add also the possibility to empty this list, removing all the dogs from this list. It means that we should update all the dogs in this list setting isFavorite to false.

If we want to use the approach of “Using NSManagedObject, we should create a NSManagedObject per dog to update and then update all of them manually.

Fortunately, CoreData provides a better way to update all the entries which satisfy the criteria of a specific predicate: NSBatchUpdateRequest.

Let’s see how to use NSBatchUpdateRequest to empty the “Favorites” list:

iOS 8+

iOS 10+

As we can see in these examples, there are four main points to update our data:

  1. Create the predicate to filter the entity to update.
  2. Set the new values with the property propertiesToUpdate. It’s a dictionary where we have the property names as keys and the new values as dictionary values. In our example, we added just an element, but this dictionary can have several elements, one per property to update.
  3. Execute the request with resultType set to updatedObjectIDsResultType.
  4. Update the main context.

Conclusion

We’ve just finished also our third adventure in the CoreData concurrency world. In the next—and last—article, we’ll see how to delete the data in a background queue. Stay tuned!