不能使用比较Magical Record中的日期的谓词 [英] Cannot use a predicate that compares dates in Magical Record

查看:113
本文介绍了不能使用比较Magical Record中的日期的谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   - (可选)我将创建一个方法来删除在上次同步时未更新的所有NSManagedObject。 void)removeStaleObjects {

//删除在此同步中未更新的对象。
NSPredicate * stalePredicate = [NSPredicate predicateWithFormat:@updated<%@,self.syncStart];
NSArray * staleObjects = [Node MR_findAllWithPredicate:stalePredicate];

for(staleObjects中的Node * n){
[[NSManagedObjectContext MR_defaultContext] deleteObject:n];
}

}

代码在MR_findAll ...

  [__ NSDate objCType]:无法识别的选择器发送到实例



我已经检查我的语法与苹果文档,我99%积极,我正确创建谓词,startDate只是

  _startDate = [NSDate date];在我的同步之前运行的

。那么在我的同步后,我调用

  [self removeStaleObjects]; 

有人知道我在搞什么?



更新:我可以通过将更新时间存储为双精度来使其正常工作。但是,我仍然有兴趣使它与NSDates工作,如果有人说出来,请张贴在这里。

解决方案

问题是属性的名称更新。它与 NSManagedObject isUpdated 方法冲突。 (请参阅父数据上下文中的核心数据NSPredicate predicateWithBlock和predicateWithFormat <



如果您重命名您的属性,一切都会按预期工作。


I am making a method that will remove all of my NSManagedObjects that were not updated in the last sync.

- (void)removeStaleObjects {

        // Delete objects that have not been updated in this sync.
    NSPredicate *stalePredicate = [NSPredicate predicateWithFormat:@"updated < %@", self.syncStart];
    NSArray *staleObjects = [Node MR_findAllWithPredicate:stalePredicate];

    for (Node *n in staleObjects) {
        [[NSManagedObjectContext MR_defaultContext] deleteObject:n];
    }

}

The code keeps failing on the MR_findAll... line with

[__NSDate objCType]: unrecognized selector sent to instance

I have checked my syntax with the apple documentation and I am 99% positive that I am creating the predicate correctly, startDate is just

_startDate = [NSDate date];

that gets run prior to my sync. then after my sync I call

[self removeStaleObjects];

Does anyone know where I am messing up?

Update: I was able to get it to work by storing the update time as a double. However, I am still interested in getting it to work with NSDates so if anyone figures something out, please post it here.

解决方案

The problem is the name "updated" of your attribute. It conflicts with the isUpdated method of NSManagedObject. (See Core Data NSPredicate predicateWithBlock and predicateWithFormat in Parent Child Context for a similar issue with a "deleted" attribute.)

If you rename your attribute, everything works as expected.

这篇关于不能使用比较Magical Record中的日期的谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆