Initializing an NSManagedObject subclass

Discussion in 'Mac Programming' started by BadWolf13, Sep 22, 2010.

  1. BadWolf13 macrumors 6502

    Dec 17, 2009
    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?
  2. kainjow Moderator emeritus


    Jun 15, 2000
    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:
  3. seepel macrumors 6502


    Dec 22, 2009
    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.
  4. BadWolf13 thread starter macrumors 6502

    Dec 17, 2009
    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?

Share This Page