Re: Core Data: undo and non-object attribute [SOLVED]
Re: Core Data: undo and non-object attribute [SOLVED]
- Subject: Re: Core Data: undo and non-object attribute [SOLVED]
- From: Matteo Rossi <email@hidden>
- Date: Sun, 15 Oct 2006 23:37:50 +0200
Oh, dear. It sounds like you still have a bit of a mess.
Surely, I have.
I was hoping you had deleted all of the code you posted. You don't
need it, and you don't need the transient attributes. They cannot
help you with your structs. The section about non-standard
attributes only deals with KVC-compliant values, i.e. objects and
NSPoint, NSSize, NSRect, or NSRange.
OK
What you want to do is to declare to perfectly normal persistent
attributes x and y. If you want to store these in instance
variables rather than the internal storage of NSManagedObject, you
can do it in two ways:
1. Simply create two instance variables with the proper names:
@interface mybasic : NSManagedObject
{
double x, y;
}
2. Keep your _point struct and implement primitive get/set accessors:
- (double)primitiveX
{
return _point.x;
}
- (void)setPrimitiveX:(double)x
{
_point.x = x;
}
That is all.
No, it can't be all. If I don't implement setX and X, setY and Y KVO
doesn't work nor undo and save.
Blindingly fast as it may be, it doesn't fire faults, so the values
may not be initialized. setPrimitiveX: is not called until the
fault is fired. Using willAccessValueForKey: and -
didAccessValueForKey: ensures that the instance variables have
valid values. If your object happens to be faulted already, methods
like sumBAD actually do work, but only as long as Core Data does
not decide to turn the object back into a fault.
Here's what I was missing: if I don't use willAccessValueForKey, the
OBJECT might not be faulted.
If you need seriously high-speed access to a list of points, you
should copy them to an old-fashioned plain-old-data C array.
Definitely, I should.
Now I have declared only two persistent double attributes x and y and
have deleted all the code about awakeFromFetch and willSave.
The code now looks like this:
...
- (double)y
{
[self willAccessValueForKey: @"y"];
double tmpValue = _point.y;
[self didAccessValueForKey: @"y"];
return tmpValue;
}
- (void)setY:(double)value
{
[self willChangeValueForKey: @"y"];
_point.y=value;
[self didChangeValueForKey: @"y"];
}
- (double) primitiveY
{
return _point.y;
}
- (void) setPrimitiveY:(double)value
{
_point.y=value;
}
I repeat, I MUST implement Y and setY, otherwise it doesn't work.
Jacob, I think I've abused of your patience too much. I really thank
you.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden