One Quickie


Inserting an object (Core Data->General)
Inserting a new object into a managed object context is a multi-stage process. This assumes that self has methods to get the managed object context and managed object model (Apple's CoreData Application template does)

Thanks to Evan Moseman who pointed me at the new easy way to do it:

        NSManagedObjectContext *moc = [self managedObjectContext];
	NSManagedObject *obj = [NSEntityDescription 
		insertNewObjectForEntityForName :@"Condition" 
		inManagedObjectContext: context];
And the older, more manual way if you need some more control over the process:
    NSManagedObjectContext *moc = [self managedObjectContext];
    NSManagedObjectModel *mom = [self managedObjectModel];

    NSEntityDescription *entity;
    entity = [[mom entitiesByName] objectForKey: @"Condition"];

    NSString *className;
    className = [entity managedObjectClassName];

    NSManagedObject *obj;
    obj = [[NSClassFromString(className) alloc]
              initWithEntity: entity
              insertIntoManagedObjectContext: moc];

    [obj autorelease];

    [obj setValue: @"nook"  forKey: @"name"];
    [obj setValue: [NSNumber numberWithInt: 23]  forKey: @"ranking"];



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com