Core Data Chapter


#1

As several others have said, I believe the core data chapter should have been larger or even broken into two separate chapters: one where the concept is introduced followed by a second where more advanced topics are presented along with a deeper discussion of core data.

One of the concepts I’m having trouble with is the multitude of objects required to obtain data. We the model, the context, the fetch request, the persistent store coordinator. I don’t understand the importance of the model object as I never see the model linked to the actual entities I thought it was supposed to represent. Why is the model important, other than because Apple says so, and when does it get hooked up to the data model we created at the beginning of the chapter? Obviously, it’s possible I have its use all mixed up. But I just don’t see what use the model object has. Or is it used internally as sort of a black box type of thing where we create it and the model object is used within the core data some place we don’t see?

Thanks.


#2

Hey man, I shared your frustration with this chapter for sure. I think it makes it all that much harder when most of this book explains everything so well and makes it “easy” for you to learn, then this chapter doesn’t make things as obvious. The upside to this is that I think it is good to have to struggle to learn at times so you can develop that discipline for the future when you won’t have people teaching you these concepts and you’ll really be on your own. That said, I hope you’ve been sticking with it and have figured some of the Core Data stuff out. I’m still learning but after I read this chapter I went searching for other resources and feel like I’ve gotten some understanding of it.

With the model, my understanding is that it is basically a mechanism by which to instantiate the persistent store coordinator, so that you can interact with the persistent store, and I think that it’s also basically putting your graphic data modeller into code, and it kinda talks with those managed object contexts that interact with the persistent store coordinator. (Side note: I read somewhere that it’s helpful to think of contexts as desktop word documents. You can add and edit things in text editor and save them to a file when you want. So the context/text editor is separate from the persistent store/file where you save the contexts objects/document.)

// Read in Homepwner.xdatamodeld model = [NSManagedObjectModel mergedModelFromBundles:nil];

The comment above “model’s” instantiation is what makes me think its a code representation of the graphic editor. Also if you alt + click the “NSManagedObjectModel” class declaration and go to the NSManagedObjectModel Class Reference, it gives you some info on what models do. It seems important to understand that entities and managed objects are not exactly the same thing, though they correspond closely. So the model helps to relate entities to managed objects which reside in managed object contexts, which are saved in persistent stores.

I know that probably wasn’t the best description and I might be off on some that but hopefully others will correct me where I’m wrong. Hope that helps you or someone else in some way.