Re: Storing Modified Dates - Core Data

Tom Hageman <trh-qWit8jRvyhVmR6Xm/[email protected]>
Newsgroups gmane.comp.macosx.devel
Organization Warty Wolfs
Message-ID <[email protected]>
Hi Trent,

On Wed, 30 Sep 2009 11:49:06 +0100, Trent Jacobs wrote:
> Hi all,
>
> I have a core data application which synchronises data with an online
> service. Synchronisation needs to be done by comparing a 'modified
> date' field.
>
> I thought I would be able to control the modified date field by doing
> the following in my base managed object class:
>
> - (void) willSave
> {
> NSDate *now = [NSDate date];
> if (false == [[self modifiedDate] isEqualToDate: now])
> {
> [self setPrimitiveValue:now forKey:@"modifiedDate"];
> }
> }
>
> This didn't work because the entity is saved whether it has been
> modified or not. So... short of adding a 'isDirty' flag, monitoring
> each setter and allowing for loading of models, is there a good way of  
> doing this? I'l all out of ideas...

In a Previous Life I did something similar in CoreData's (grand)parent  
EOF.  (I have not actually worked with CoreData yet, so I'm not sure how  
well this translates; I hope that the concepts are similar enough to get  
you an idea how you might proceed.)

It involved observing the EOObjectsChangedInEditingContextNotification,  
checking each object from the list of updated objects passed in by the  
notification for modifications against its committedSnapshot, ie. the  
last-saved version of the object (which was surprisingly involved to do  
efficiently, esp. trying to avoid firing relationship faults.  It is  
relatively straightforward if you only need to consider changes in  
attribute values and can ignore relationship changes), only recording a  
new modifiedDate value when any attribute had actually changed _and_ the  
object actually had a modifiedDate attribute (as determined from its  
committedSnapshot).

HTH,
Regards,
--
Tom.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.