这可以在其他地方回答,但我无法找到答案。我有一个Speaker
实体与Speech
个对象有很多关系。如何编写一个谓词,该谓词将获取只有Speakers
属性Speech
个对象的所有audioURL
?
我尝试了一些事情,基本上调整了以下谓词:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Speaker"];
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat:@"speech.audioURL != nil AND speech.audioURL != ''"];
答案 0 :(得分:1)
换句话说,您想要提取Speakers
,其Speech
个对象中没有audioURL
==""他们的Speech
个对象中没有audioURL
== nil:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Speaker"];
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat:@"(NONE speech.audioURL == nil) AND (NONE speech.audioURL == '')"];
修改强>
但是经过测试,CoreData似乎将NONE解释为AN NOT而不是ALL NOT。因此,请使用SUBQUERY:
predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(speech, $s, $s.audioURL == nil OR $s.audioURL == '').@count == 0"];
这可以处理Speaker
的演讲(如果存在)符合条件的情况。如果您还想排除那些没有演讲的演讲者,请添加另一个子句:
predicate = [NSPredicate predicateWithFormat:@"(speech.@count > 0) AND (SUBQUERY(speech, $s, $s.audioURL == nil OR $s.audioURL == '').@count == 0)"];