Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data returns NSManagedObject instead of Concrete class, but only when using . accessor

I have set up a Core Data model where I have two objects, say Person and Address. A person has an address, and an address can belong to many people. I have modelled it in core data as such (so the double arrow points to Person, while the single arrow goes to Address)

I have then created two classes for those objects, and implemented some custom methods in those classes. In the Core Data model I have entered the names of the classes into them.

If I fetch an Address from Core Data directly, it gives me the actual concrete class and I can call my custom methods on it.

If on the other hand I fetch a Person and try to access the Address through Person (eg: person.address) I get back an NSManagedObject that is an address (eg: I can get to all the core data attributes I've set on it) but it doesn't respond to my custom methods, because it's of type NSManagedObject instead of Address. Is this a limitation of Core Data or am I doing something wrong? If it is a limitation are there any work arounds?

like image 602
rustyshelf Avatar asked Oct 16 '09 05:10

rustyshelf


1 Answers

Did you create those classes using the modeller (Select an Entity, File > new file.., Managed Object Class, then select the Model Entity)?

A while ago I had a similar problem because I didn't create my managed object models using the Modeller. What I did to make sure everything was up and running was to copy and save my custom methods (and everything else I'd implemented) and start from scratch using the modeller. Then I was able to customize my model classes again and everything worked just fine.

I know this is not a complete answer but perhaps it can help you until someone explains exactly what is going on.

Cheers!

like image 167
Lio Avatar answered Sep 19 '22 10:09

Lio