ios – NSManagedObject timeStamp更新

ios – NSManagedObject timeStamp更新,第1张

概述我想跟踪NSManagedObject属性的更改,以保持NSData * lastUpdate属性“最新” 当NSManagedObject更改其属性时,有几种方法可以获得Notified I.首先是覆盖要跟踪的所有属性的setter方法.在NSManaged对象中相当复杂 – 检查它here II.第二个可能是一个好的.你可以覆盖“didChangeValueForKey”方法,在每次属性更改时 我想跟踪NSManagedobject属性的更改,以保持NSData * lastUpdate属性“最新”

当NSManagedobject更改其属性时,有几种方法可以获得NotifIEd

I.首先是覆盖要跟踪的所有属性的setter方法.在NSManaged对象中相当复杂 – 检查它here

II.第二个可能是一个好的.你可以覆盖“dIDChangeValueForKey”方法,在每次属性更改时都会调用它.

-(voID)dIDChangeValueForKey:(Nsstring *)key{    [super dIDChangeValueForKey:key];    NSLog(@"Value for key:%@ has changed",key);}

不幸的是,由于文档说明……我们不应该覆盖此方法:

“You must not overrIDe this method.”

III.键值观察引导我们回到IInd方法,重写“dIDChangeValueForKey”.

UPD.
IV.我试图覆盖-willSave方法

-(voID)willSave{   NSArray *observedKeys = @[@"name",@"imageType"];   NSDictionary * changesALL = self.changedValues;   for (ID key in changesALL){       if ([observedKeys containsObject:key]){           self.lastUpdate = [NSDate date];           NSLog(@"updated For key: %@",key);        }    }}

这导致了不定式循环,在文档中有描述.
(这里描述了正确的方法,所以我已经回答了这个问题)

If you want to update a persistent property value,you should typically test for equality >of any new value with the existing value before making a change. If you change property >values using standard accessor methods,Core Data will observe the resultant change >notification and so invoke willSave again before saving the object’s managed object >context. If you continue to modify a value in willSave,willSave will continue to be called >until your program crashes.

For example,if you set a last-modifIEd timestamp,you should check whether either you >prevIoUsly set it in the same save operation,or that the existing timestamp is not less >than a small delta from the current time. Typically it’s better to calculate the timestamp >once for all the objects being saved (for example,in response to an >NSManagedobjectContextwillSaveNotification).

解决方法 适合您的用例的解决方案,以覆盖willSave方法并使用它来设置新的lastUpdated值.在将脏对象保存到上下文之前,会自动调用此方法.

如果需要验证脏的内容,可以使用changedValues属性的内容.

总结

以上是内存溢出为你收集整理的ios – NSManagedObject timeStamp更新全部内容,希望文章能够帮你解决ios – NSManagedObject timeStamp更新所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1057400.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存