views:

302

answers:

2

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?

+2  A: 

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!

Lio
Thanks for the tip, I did try that on just the one entity, but I will do it to all of them and see if it makes any difference
rustyshelf
That worked! I recreated them all from the model, added my custom code back in and BAM it was happy. Weird. I can't really see any obvious difference apart from the extra methods it generated that I didn't have...
rustyshelf
+1  A: 

You probably just forgot to set the name of the class in the model when you created the entity - it defaults to NSManagedObject. Click on Person and Address in the modeller and check, on the far right side where the Entity properties are listed, that the Class field is filled in correctly with the name of the corresponding objective C class and isn't just the default NSManagedObject setting.

Terry Longrie
Nope I definitely have set all those, hence why it works if I fetch an object of that type directly from core data
rustyshelf
Actually, fetching an object will work whether the class of the entity is set to NSManagerObject or not. But, as @Terry Longrie points out, for the custom methods of a managedObject to be called, the class of the entity needs to be explicitly set to Person or Address or whatever. When you create the custom class using the modeler as @Lio suggested, this is automatically done for you. When you create the custom class by hand, so to speak, you need to specify the class by hand as well. Wasted some hours of my life figuring that out.
Elise van Looij