ios – 在NSManagedObjectsDidChangeNotification创建无限循环后设置lastModificationDate属性

ios – 在NSManagedObjectsDidChangeNotification创建无限循环后设置lastModificationDate属性,第1张

概述我在我的所有实体中添加了一个lastModifiedDate属性,以避免在将UIManagedDocument与iCloud同步时出现重复,我发现如果我使用离线设备(iPad)创建新实体,并且同时创建相同的实体,就会发生这种情况使用其他在线设备(iPhone). 我想在对象发生变化时设置此属性,因此我订阅了NSManagedObjectContextObjectsDidChangeNotifica 我在我的所有实体中添加了一个lastModifIEdDate属性,以避免在将UIManageddocument与iCloud同步时出现重复,我发现如果我使用离线设备(iPad)创建新实体,并且同时创建相同的实体,就会发生这种情况使用其他在线设备(iPhone).

我想在对象发生变化时设置此属性,因此我订阅了NSManagedobjectContextObjectsDIDChangeNotification.我编写的用于设置lastModifIEdDate的代码创建了一个无限循环,因为通过设置lastModificationDate属性,它创建了一个更改,NSManagedobjectContextObjectsDIDChangeNotification将再次通知该等更改…

有可能解决它吗?有没有更好的方法来实现我的目标?我应该将managedobjectContext子类化并覆盖willSave:?

//At init...[[NSNotificationCenter defaultCenter] addobserver:applicationDatabase                                             selector:@selector(objectsDIDChange:)                                                 name:NSManagedobjectContextObjectsDIDChangeNotification                                               object:applicationDatabase.manageddocument.managedobjectContext];(voID) objectsDIDChange: (NSNotification*) note{  // creates an infinite loop    NSDate *dateOfTheLastModification = [NSDate date];    NSMutableArray *userInfoKeys = [[note.userInfo allKeys] mutablecopy];    for(int i=0; i< userInfoKeys.count;i++){        Nsstring *key = [userInfoKeys objectAtIndex:i];        if([key isEqualToString:@"managedobjectContext"]){            [userInfoKeys removeObject:key];        }    }    for(Nsstring *key in userInfoKeys){        NSArray *detail = [note.userInfo objectForKey:key];        for (ID object in detail){            [object setValue:dateOfTheLastModification forKey:@"lastModifIEdDate"];        }}
解决方法 要避免无限循环,可以使用.设置上次修改日期
原始访问者:

[object setPrimitiveValue:dateOfTheLastModification forKey:@"lastModifIEdDate"];

因为这不会触发另一个“更改”通知.但这也意味着
没有观察者会看到变化.

覆盖托管对象子类中的willSave会遇到同样的问题.
willSave的Apple文档说明:

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).

所以你应该注册NSManagedobjectContextwillSaveNotification,
并在托管对象中的所有更新和插入的对象上设置时间戳
上下文.注册的方法可能如下所示:

-(voID)contextwillSave:(NSNotification *)notify{    NSManagedobjectContext *context = [notify object];    NSDate *dateOfTheLastModification = [NSDate date];    for (NSManagedobject *obj in [context insertedobjects]) {        [obj setValue:dateOfTheLastModification forKey:@"lastModifIEdDate"];    }    for (NSManagedobject *obj in [context updatedobjects]) {        [obj setValue:dateOfTheLastModification forKey:@"lastModifIEdDate"];    }}

这假定您的所有实体都具有lastModifIEdDate属性,否则你必须检查对象的类.

总结

以上是内存溢出为你收集整理的ios – 在NSManagedObjectsDidChangeNotification创建无限循环后设置lastModificationDate属性全部内容,希望文章能够帮你解决ios – 在NSManagedObjectsDidChangeNotification创建无限循环后设置lastModificationDate属性所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存