Leonardo
Leonardo

Reputation: 9857

Keeping track of change in a CoreData entity

I have a simple iOS application that make use of CoreData. For a particular reason I need to display an history records in a UITableView based on Entity values.

I explain better, supposing I have entity A with field of value x, then at a given date I changed value of A with xy.

In UITableView, I expect to see two records, grouped by date of change.

25/07/2011
A=x
28/07/2011 
A=xy

I am still thinking with SQL in mind, which is obviously wrong regarding CoreData, so the first solution coming to my mind would be to add a new entity maybe with a relationship to A. So I wonder what's the best CoreData approach for this, maybe there's a convenient mechanism to save and recall a snapshot of an entity or a specific attribute at a given datetime.

thanks

Upvotes: 2

Views: 865

Answers (1)

srgtuszy
srgtuszy

Reputation: 1548

You can acheive this with KVO (Key Value Observing). Every NSManagedObject subclass can override:

- (BOOL)validateValue:(id *)value forKey:(NSString *)key error:(NSError **)error

You can override these as well, but not encouraged to:

- (void)didChangeValueForKey:(NSString *)key or

- (void)willChangeValueForKey:(NSString *)key

methods, which gets call every time the value gets changed for a particular keypath. I belive this will solve your problem. Take a look at KVO Programming Guide to get a more deep understanding of KVO subject @ http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html .

Upvotes: 2

Related Questions