Contents

CoreData: CRUD With Concurrency In Swift - Part 3


Do you have performance issues with your CoreData implementation because you’re blocking the main thread with some computations? This series can help you to solve your problems and improve the user experience of your App. Third Part: Update.

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!

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+

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
let dogToUpdate = // `Dog` object to update

privateManagedObjectContext.perform {
	// Creates a queue-safe `dog` object as said in part two
	guard let queueSafeDog = privateManagedObjectContext.object(with: dogToUpdate.objectID) as? Dog else { return }
	// Sets dog as favorite
	queueSafeDog.isFavorite = true

	do {
		// Saves the entry updated
		try privateManagedObjectContext.save()

		// Performs a task in the main queue and wait until this tasks finishes
		mainManagedObjectContext.performAndWait {
			do {
				// Saves the data from the child to the main context to be stored properly
				try mainManagedObjectContext.save()
			} catch {
				fatalError("Failure to save context: \(error)")
			}
		}
	} catch {
		fatalError("Failure to save context: \(error)")
	}
}

iOS 10+

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
let dogToUpdate = // `Dog` object to update
	
persistentContainer.performBackgroundTask { privateManagedObjectContext in
	// Creates a queue-safe `dog` object as said in part two
	guard let queueSafeDog = privateManagedObjectContext.object(with: dogToUpdate.objectID) as? Dog else { return }
	// Sets dog as favorite
	queueSafeDog.isFavorite = true

	do {
		// Saves the entry updated
		try privateManagedObjectContext.save()
	} catch {
		fatalError("Failure to save context: \(error)")
	}
}

When we update a NSManagedObject object, then we must save these changes manually—like in the examples below—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+

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
privateManagedObjectContext.perform {
	// Creates new batch update request for entity `Dog`
	let updateRequest = NSBatchUpdateRequest(entityName: "Dog")
	// All the dogs with `isFavorite` true
	let predicate = NSPredicate(format: "isFavorite == true")
	// Assigns the predicate to the batch update
	updateRequest.predicate = predicate

	// Dictionary with the property names to update as keys and the new values as values
	updateRequest.propertiesToUpdate = ["isFavorite": false]

	// Sets the result type as array of object IDs updated
	updateRequest.resultType = .updatedObjectIDsResultType

	do {
		// Executes batch
		let result = try privateManagedObjectContext.execute(updateRequest) as? NSBatchUpdateResult

		// Retrieves the IDs updated
		guard let objectIDs = result?.result as? [NSManagedObjectID] else { return }

		// Iterates the object IDs
		objectIDs.forEach { objectID in
			// Retrieve a `Dog` object queue-safe
			let dog = mainManagedObjectContext.object(with: objectID)

			// Updates the main context
			mainManagedObjectContext.refresh(dog, mergeChanges: false)
		}
	} catch {
		fatalError("Failed to execute request: \(error)")
	}
}

iOS 10+

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
persistentContainer.performBackgroundTask { privateManagedObjectContext in
	// Creates new batch update request for entity `Dog`
	let updateRequest = NSBatchUpdateRequest(entityName: "Dog")
	// All the dogs with `isFavorite` true
	let predicate = NSPredicate(format: "isFavorite == true")
	// Assigns the predicate to the batch update
	updateRequest.predicate = predicate

	// Dictionary with the property names to update as keys and the new values as values
	updateRequest.propertiesToUpdate = ["isFavorite": false]

	// Sets the result type as array of object IDs updated
	updateRequest.resultType = .updatedObjectIDsResultType

	do {
		// Executes batch
		let result = try privateManagedObjectContext.execute(updateRequest) as? NSBatchUpdateResult

		// Retrieves the IDs updated
		guard let objectIDs = result?.result as? [NSManagedObjectID] else { return }

		// Updates the main context
		let changes = [NSUpdatedObjectsKey: objectIDs]
		NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes, into: [mainManagedObjectContext])
	} catch {
		fatalError("Failed to execute request: \(error)")
	}
}

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!