Posts

Showing posts from November, 2017

Core Data and Concurrency

Image
Introduction :  Up to now, we've used a single managed object context, which we created in the  CoreDataManager  class. This works fine, but there will be times when one managed object context won't suffice. What happens if you access the same managed object context from different threads? What do you expect happens? What happens if you pass a managed object from a background thread to the main thread? Let's start with the basics. Concurrency Basics Before we explore solutions for using Core Data in multithreaded applications, we need to know how Core Data behaves on multiple threads. The documentation is very clear about this. Core Data expects to be run on a single thread. Even though that thread doesn't have to be the main thread, Core Data was not designed to be accessed from different threads. Core Data expects to be run on a single thread. The Core Data team at Apple is not naive, though. It knows that a persistence framework needs to be accessible