开发者

How to correctly implement a custom initializer for a subclassed NSManagedObject

开发者 https://www.devze.com 2023-03-09 21:15 出处:网络
I am wondering what the correct way is to create my own initializer of a class that is subclassing 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

精彩评论

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