CoreData获取所有关系属性为True的位置

时间:2016-02-10 15:51:07

标签: swift core-data nspredicate nsfetchrequest

我有两个NSManagedObjects,如下所述。

extension Day {
    @NSManaged var date: NSDate?
    @NSManaged var isHighPriority: NSNumber?
    @NSManaged var projects: NSSet?
}

extension Project {
    @NSManaged var complete: Bool
    @NSManaged var title: String?
    @NSManaged var day: Day?
}

我想要做的是获取完成所有Day的所有Projects个对象。但是,当我尝试此获取请求时,我收到此错误。 Exception was caught during NSPersistentStoreCoordinator -performBlock: Unsupported predicate

这是我的谓词

let predicate = NSPredicate(format: "isHighPriority == %@ AND ALL projects.complete == TRUE", NSNumber(bool: true))

如何正确获取集合中每个Day已完成的所有Project?我可以获取然后filter,但我宁愿在CoreData级别完成所有操作。

1 个答案:

答案 0 :(得分:0)

我可以通过在SUBQUERY

中执行NSPredicate来执行此操作
let predicate = NSPredicate(format: "isHighPriority == %@ AND SUBQUERY(projects, $project, $project.complete == TRUE).@count == projects.@count", NSNumber(bool: true))