Mastering In CoreData (Part 12 Multithreading Concurrency Problem)

Core Data

In part 2 Core Data stack we said

Managed Object Context (MOC)

It’s sit on top of Core Data stack and your application will interact with the most, and therefore it is the one that is exposed to the rest of your application

Application can create one or more MOC per stack. Context is connected to persistent store coordinator or from parent context

Rule Of thumb : You can’t achieve concurrency in Core Data without using multiple context.

Concurrency Problem

Let’s suppose we have two managed Object Context we called Managed Object Context A and Managed Object Context B both request User data from the persistent store as shown in Figure 1. User Entity has count property.

Managed Object Context A private context that is processing server data

Managed Object Context B representing UI data

Figure 1

In the Figure 2 persistent store sends User Entity data to both the contexts

Figure 2

As shown in Figure 3 Managed Object Context A updated count value to 2 from the data that comes from server and pushed that changes to persistent Store Coordinator to Persistent Store using save() method on the context. Managed Object Context B doesn't know that changes still and still have older count value which is a problem

In part 2 Core Data stack we said

Managed Object Context (MOC)

When an operation on such objects is completed, you save such MOC, typically down to the Persistent Store coordinator to persistent store

Figure 3

As shown in Figure 4 since NSManagedObjectContext B is unaware of this server update . When user update value from client side when NSManagedObjectContext B tries to update value it again pushed count value to 2 which is wrong

Figure 4

We need multiple Managed Object Contexts if you perform Core Data operations on multiple threads. The caveat, however, is that managed object contexts are unaware of each other’s existence. Changes made to a managed object in one managed object context are not automatically propagated to other managed object contexts. How do we solve this problem?

Solution

There are two popular strategies that Core Data supports, notifications and parent-child managed object contexts

Summary

In this part 12 we looked the concurrency problem in the Core data

What Next?

In the next part we will look how to solve the concurrency problem using Concurrency Strategy that Core data Provides

Useful Links

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/Concurrency.html

https://medium.com/shakuro/introduction-to-ios-concurrency-a5db1cf18fa6

https://cocoacasts.com/swift-and-cocoa-fundamentals-threads-queues-and-concurrency
https://developer.apple.com/documentation/coredata/using_core_data_in_the_background

https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext

https://medium.com/@marcosantadev/core-data-notifications-with-swift-acc8232a674e

https://marcosantadev.com/core-data-notification-swift/

https://www.youtube.com/watch?v=_QolYhiKWvU

Senior iOS Engineer | HungerStation | Delivery Hero

Senior iOS Engineer | HungerStation | Delivery Hero