PDA

View Full Version : Initializing an NSManagedObject subclass




BadWolf13
Sep 22, 2010, 02:42 PM
I've been programming with Obj-C for a few months now, but I'm new to this whole Core Data thing. Now so far, most of my model objects have required that I override the (id)init method, so as to properly initialize the object. Now so far in the tutorials, the book I've been working through, and examples online, I've only seen one person override the (id)init method in a subclass of NSManagedObject. In this example, instead of calling

[super init]

they called

[self initWithEntity: tEntityDescription insertIntoManagedObjectContext: theMOC];

Is this a required step in overriding the (id)init with a NSManagedObject subclass, or would I still be good with the way I've done with non-core data objects?



kainjow
Sep 22, 2010, 08:57 PM
Yes, you need to use the initWithEntity: method. You should be to able use init in your subclass, just make sure you call that method on self/super.

The docs say:
Important: This method is the designated initializer for NSManagedObject. You should not initialize a managed object simply by sending it init.

seepel
Sep 23, 2010, 03:06 AM
You can overrride init, but I would be careful, you'll need some sort of shared NSManagedObjectContext which will be trouble if you want to do any threading.

BadWolf13
Sep 23, 2010, 01:45 PM
You can overrride init, but I would be careful, you'll need some sort of shared NSManagedObjectContext which will be trouble if you want to do any threading.

Sorry, I'm kind of lost with what you're saying here. My understanding is that the NSManagedObjectContext looks over the objects, so why/how would I share it, and what exactly would I be sharing it with?