Core data: ivars and other stuff

Sunday October 15th, 2006

After playing for a while with CD, I decided it fitted me perfectly but… Speed ! CD is indeed very fast and highly optimized but what if you should draw many times per second graphic objects which store their coordinates through core data ? I mean, I draw lines (entities) connecting joints (other kind of entities). If you want best performance you should avoid store the joint coordinates (x,y) in core data since each access goes through a great number of method invocations. Yes I know there’s NSPoint, but I’m simplyfying since my point is 3d (z, where are you ?).
Docs about ivars are not that good. Here’s the way. First, you have to create two persistent attributes x,y. The implementation follows:
– (double) primitiveX
{
return _point.x;
}
– (void) setPrimitiveX:(double)value
{
_point.x=value;
}

– (double)x
{
[self willAccessValueForKey: @”x”];
double tmpValue = _point.x;
[self didAccessValueForKey: @”x”];

return tmpValue;
}

– (void)setX:(double)value
{
[self willChangeValueForKey: @”x”];
_point.x=value;
[self didChangeValueForKey: @”x”];
}

– (double)y
{
[self willAccessValueForKey: @”y”];
double tmpValue = _point.y;
[self didAccessValueForKey: @”y”];

return tmpValue;
}

– (double) primitiveY
{
return _point.y;
}
– (void)setY:(double)value
{
[self willChangeValueForKey: @”y”];
_point.y=value;
[self didChangeValueForKey: @”y”];
}

– (void) setPrimitiveY:(double)value
{
_point.y=value;
}

Some important notes. Core data relies on primitive accessors to manage undo/redo operatons and some other stuff. So, you must implement setPrimitive accessors, since CD attribute names (x and y) are different from ivars’ ones (_point.x and _point.y). If you don’t, you don’t get undo/redo working.Second, each default value initialization must occur in -(void) awakeFromInsert, which is called once. This is the right place even for addObserver. You shouldn’t override initWithEntity: …. On the other hand, each deallocation, or removal of observers must be in -(void) didTurnIntoFault.

Go top