Assigning NSManagedObject properties before insertion doesn't stick
===

I'm trying to assign the property values of an NSManagedObject before inserting them into the context using a dictionary. The issue I'm running into is that the object values get zero'd out after insertion.

I'm working according to the documentation on:

NSManagedObject
----------

    - (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context

NSManagedObject uses dynamic class generation to support the Objective-C 2 properties feature (see “Declared Properties”) by automatically creating a subclass of the class appropriate for entity.initWithEntity:insertIntoManagedObjectContext: therefore returns an instance of the appropriate class for entity. The dynamically-generated subclass will be based on the class specified by the entity, so specifying a custom class in your model will supersede the class passed to alloc.

If context is not nil, this method invokes `[context insertObject:self]` (which causes awakeFromInsert to be invoked).

----------

This implies that I can create an NSManagedObject outside of a context, perform any necessary operations and then insert the object myself using `[context insertObject:self]`

Using this, I implemented for the NSManagedObject as follows:

    -(id)initWithDictionary:(NSDictionary *)dict withEntityForName:(NSString *)name insertIntoContext:(NSManagedObjectContext *)context {

	    NSEntityDescription *entity = [NSEntityDescription entityForName:name inManagedObjectContext:context];
	    self = [self initWithEntity:entity insertIntoManagedObjectContext:nil];
	    if (self != nil) {
                     //Assign all primitive properties
		     [self primitiveRefreshFromDictionary:dict];
		     if (context != nil)
			    [context insertObject:self];
	    }
	    return self;
    }

According to my logs, the object is successfully initialized and assigned all it's properties, but once fetched, all the objects properties were nil. So I read some more, and found:

> awakeFromInsert 

> Invoked automatically by the Core Data framework when
> the receiver is first inserted into a managed object context.

    - (void)awakeFromInsert

> You typically use this method to initialize special default property
> values. This method is invoked only once in the object's lifetime.

I figured that if there were any place that values would be zero'd out, this would be it. Imagine my surprise when I implemented

    -(void)awakeFromInsert {
    	if (![self primitiveValueForKey:@"wid"]) {
    		NSLog(@"Nil values here...");
    		[super awakeFromInsert];
    	}
    }

And the logs never showed up. And yet, after an insert, all properties are nil (later modification of values stick).

So, my question is, how can I make property values before insertion stick _or_ carry that dictionary into awakeFromInsert so that I can assign values there (since documentation says that's where they should be assigned).

Before I get the questions on "why", here's the answer. I need to minimize as much overhead as humanly possible. Everything is intensely resource heavy as it is, especially being connected to a UITableView through an NSFetchedResultsController. Very common events trigger 12 insertions + 15 property changes to each object. The biggest plus I get from this is decreasing the inane amount of pings I get to:

    -controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:

especially for Move events caused by changes in the sorted properties.

Any help greatly appreciated.

Cheers, Z