views:

88

answers:

2

How to group methods belong to one entity in one class file in Core Data like Entity Framework?

In Linq, we can put all methods in the domain object class and reuse them, in Core Data, is there any way to create different classes for different entities? Or we can only use predicate to retrieve?

It seems that I can't define the class for each entity I configured in the data model. And it is not like hibernate that I can control the physical database schema via ORM. Correct me if I am wrong, I don't believe Core Data can do this as far I know.

So what is the advantage to use Core Data? And is there any mechanism on Cocoa that I can define my domain object classes including primary key, foreign key, for instance, and then create the database schema then?

It seems like the Core Data can only support configuring the objects but there is even no way to configure the physical database via the Core Data.

A: 

Look at the docs for KVC ("key-value-coding"). You use key paths to obtain the values you want. You can also sort the data in an array using a key path. For that, take a look at NSSortDescriptors. Otherwise, yes, just change your NSPredicate and re-fetch.

Matt Long
+3  A: 

In your Core Data model you can define which class to instantiate your entities as:

alt text

When Core Data fetches objects from the store, it will attempt to create them as instances of this class (although it's not quite that simple, Core Data does some tricks).

You can auto-generate class files for your entities by opening your core data model in a new window, and choosing File->New File:

alt text

You should see a new "Managed Object Class" item:

alt text

Choose this and select the entities to generate classes for:

alt text

A class will be generated with various properties for getting/setting the attributes on your entity:

alt text

If you want to add your own methods, I would recommend you add them to a separate Category. This is because you will often want to regenerate these core data classes when you add new properties or change the entities. If you make additions directly to these files you will lose your changes the next time you generate them.

So create a category called Person+Additions or whatever name you think is appropriate:

alt text

And add any new methods or properties to this class:

alt text

Note that you cannot add new instance variables, but NSManagedObject has facilities to get/set values in an internal dictionary. See the NSManagedObject documentation

So once you've done all this, all you do is cast the NSManagedObject you get from a fetch request (or wherever) to the appropriate type:

#import "Person+Additions.h"

//...

Person *p = (Person *) [fetchResult objectAtIndex:0];
[p myCustomMethod];

//...

As for your other questions about primary keys, and interacting with the physical database, this isn't really what Core Data is designed to provide you. You should not think too much about the underlying database, Core Data is meant to be more high level than that. You define your model, and fetch objects through the API. Primary keys and othe "database-y" details like this are not exposed.

If you want full control over the database you should use the sqlite APIs. But I would say for the large majority of applications Core Data is the better choice.

Mike Weller
+1 Nicely explained.
TechZen