Re: Core Data : awakeFromInsert
Re: Core Data : awakeFromInsert
- Subject: Re: Core Data : awakeFromInsert
- From: "Marcus S. Zarra" <email@hidden>
- Date: Thu, 9 Mar 2006 12:54:11 -0700
Just a thought but I would store the value in the transaction and
then create a class level method that builds the transaction for you:
+ (id)transactionForStock:(id)stock
{
id transction = [NSEntityDescription
insertNewEntityForName:@"Transaction ...
[transaction setValue:stock forKey:@"stock"];
[transaction setValue:[stock valueForKey:@"price"] forKey:@"price"];
return transaction;
}
Then instead of creating the transaction from an NSEntityDescription
call this class method instead.
Marcus
P.S. Typed into mail, guaranteed not to compile as shown
On Mar 9, 2006, at 12:46 PM, WhiteContainer wrote:
Hi,
About a Core Data document-based application...
3 entities A_stock<-->>B_transaction<<-->C_invoice
I want to keep an history of the prices for the sold stock items.
That happens in B (transactions=items sold).
What I want : If the user changes the price (persistent attribute)
of an item in A, the transaction prices (transient and dependant
from the A value via a calculation trough the relationship) must
not change. One item can be sold at different prices during his
life-time in the stock (discount, gifts etc).
So for each transaction in B, I have to "snapshot" the stock price
attribute at the moment of the sale, that means at the moment of
the creation of a B instance.
Ideas and tests (that did not work...because of me fore sure...):
1-shut off the notification in A_stock with
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)theKey
////// it did'nt work
2-copy the value and stock it in a persistent attribute as a value
totally independant from A_stock
/////i don't know how to do exactly
3-awakeFromInsert
I think it is the best solution : it's perfect for time stamps and
id's.
But here, there is a problem : the value in transaction depends on
the relationship B>A and relationships are not set up at this
moment...
In the Xcode example iClass, look at how the developer reached in
Person.m, the fonction getId from the entity IDgenerator, which is
not related at all with person in the model :
>>>>>>>>
@implementation Person
- (void)awakeFromInsert {
[self setPrimitiveValue: [[[[NSApplication sharedApplication]
delegate] idGenerator] getNewID] forKey: @"personID"];
}
@end
@implementation iClassAppDelegate
- (IDGenerator *)idGenerator {
return idGenerator;
}
@end
@implementation IDGenerator
// This is unmodeled, because otherwise we get caught in a
validation loop before saving
// since accessing the property during validation would cause it to
change
- (NSNumber *)getNewID {
Some Stuff
}
@end
>>>>>>>>>>>>>>>
My actual code in my test project :
>>>>>>>>>>>>>>>>>>>>>
@implementation Stock
-(NSNumber*)givePrice
{
return [NSNumber numberWithDouble:[[self valueForKey:@"stockPrice"]
doubleValue]];
}
@end
@implementation Transaction
- (void)awakeFromInsert {
[super awakeFromInsert];
[self setValue: [//////which path???to reach://// givePrice]
forKey: @"transactionFinalPrice"];
}
@end
>>>>>>>>>>>>>>>>>>>>
Many, many, many thx in advance if you can concretely help me...
Best Regards
_______________________________________________
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
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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