我不确定如何将几个条件"级联"到NSPredicate中.
我对Core Data很新,不确定这是否是实现我想要做的正确方法.
这是我想要做的:
有一个Photo对象与Location对象具有whereTook关系,该对象具有photosTookThere的反转.
Photo依次具有名为isFavourite的NSNumber属性.
我想配置一个NSFetchRequest,它将首先检查photosTookThere是否存在,如果是,请检查每个生成的Photo对象并仅返回设置为收藏夹的对象.
这是迄今为止的代码:
request.entity = [NSEntityDescription entityForName:@"Location" inManagedObjectContext:context];
request.sortDescriptors = [NSArray arrayWithObject[NSSortDescriptorsortDescriptorWithKey:@"locationId" ascending:YES]];
request.predicate = [NSPredicate predicateWithFormat:@"photosTookThere.@count != 0"];
Run Code Online (Sandbox Code Playgroud)
如何将第二个条件级联到谓词中并返回正确的结果?
有没有特定的方法来配置NSPredicate来比较日期?
基本上我有一个Photo对象有一个NSDate,lastViewed.
我想配置一个NSPredicate,它将返回最近查看的所有Photo对象,而不是指定的时间段 - 通常为两天.
我这样得到过去的日期:
NSTimeInterval secondsPast = -172800;
NSDate * twoDaysPast = [NSDate dateWithTimeInterval:secondsPast sinceDate:[NSDate date]];
request.predicate = [NSPredicate predicateWithFormat:@"lastViewed > %@", twoDaysPast];
NSTimeInterval secondsPast = -172800;
NSDate * twoDaysPast = [NSDate dateWithTimeInterval:secondsPast sinceDate:[NSDate date]]; 并且因此配置NSPredicate:
request.predicate = [NSPredicate predicateWithFormat:@"lastViewed > %@", twoDaysPast];
但是我没有得到任何结果,我不确定为什么.
我知道我的所有Photo对象都有lastViewed设置 - 只要将Photo添加到Core Data,它就会设置为默认值now,所以默认情况下我应该看到创建为lastViewed的每张Photo都比我的twoDaysPast NSDate更新.
我可以用这种方式直接比较两个NSDate实例吗?