Initializing an NSManagedObject subclass

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

  1. macrumors 6502

    Joined:
    Dec 17, 2009
    #1
    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

    Code:
    [super init]
    they called

    Code:
    [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. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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. macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #3
    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. thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #4
    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