开发者

How to correctly implement a custom initializer for a subclassed NSManagedObject

I am wondering what the correct way is to create my own initializer of a class that is subclassing NSManagedObject.

Currently I am initializing like this:

-(id)initWithXML:(TBXMLElement *)videoXML
{
    // Setup the environment for dealing with Core Data and managed objects
    HenryHubAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSEntityDescrip开发者_JAVA技巧tion *entityHubPieceVideo = [NSEntityDescription entityForName:@"HubPieceVideo" 
                                                           inManagedObjectContext:context];

    self = [[HubPieceVideo alloc] initWithEntity:entityHubPieceVideo insertIntoManagedObjectContext:context];
    // do stuff and then save

    NSError *error;
    if(![context save:&error]) 
    {
        NSLog(@"HubPiece video save context error: %@ %@", error, [error userInfo]); 
    }
}

Seems like some others also do it this way.


Just found that the NSManagedObject reference says:

If you instantiate a managed object directly, you must call the designated initializer (initWithEntity:insertIntoManagedObjectContext:).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜