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:).
精彩评论