Previous episode: 04. Managed Object Context
Next episode: 06. Setting Up the Stack
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
So far, you know that core data represents models using entities as managed object models. You also know that it creates these inside of a managed object context to create the object graph. As you modify the graph, the context keeps track of these changes, but what does it do with those changes? That's where the next part of the stack comes in. The persistent store coordinator. The persistent store coordinator, represented as an instance of NSPersistentCoordinator, sits in the middle of the stack between the managed object context and the underlying persistent store and facilitates communication between the two. While the context is responsible for keeping track of changes made to copies of the managed object model, it's the coordinator's job to create instances of the actual entities to find in the model. Going back to the library app example, a user could add a new book to the app and fill out some details. This action would add a new instance of the book entity to the context, along with any relationships it has. Because the context keeps track of the existing object graph, it inserts this book and its relationships in a way that maintains its integrity. At that point, there's no real data backing this new book in the underlying store. If the user were to hit save at this point, then the context would hand off these changes to the persistent store coordinator, who then creates new instances of the entities. Similarly, if the user were to browse their list of existing books, the persistent store coordinator would be responsible for retrieving existing instances from a persistent store. To bring it all together, the managed object model defines the structure of the data, and the persistent store coordinator realizes objects from the data in the persistent store and passes those objects off to the requesting context. In design patterns, a facade is an object that serves as an interface hiding a more complex implementation underneath. This is exactly the role the coordinator plays. When using core data, the data might be stored across multiple data stores. Some data may reside on disk, while other data may exist only in memory. The coordinator hides all this away. From the perspective of the managed object context, there's just a single aggregate data store. When you save multiple versions of your managed object model, something we won't be getting into in this course, these are stored across different data stores and the coordinator handles the communication and logic to interact with the various stores. A few iOS versions ago, you had to create all of this on your own, but these days, it's just a few lines of code and that's because there's one object to rule them all. In the next video, let's build the stack using a persistent container.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.