我有一个像下面这样的核心数据模型的结构:NSPredicate和核心数据:检索关系中的对象
Product <-->> OrderProduct where the relationship (from Product to OrderProduct) is called productOrders while the inverse is called product
Order <-->> OrderProduct where the relationship is called orderProducts while the inverse is called order
Client <-->> Order where the relationship is called orders while the inverse is called client
在一个UIViewController
我使用的是下面的断言相关的NSFetchRequest
:
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"(ANY productOrders.order.client.code == %@)", clientCode];
谓词运作良好。我能够检索与特定客户关联的一个(或多个)订单的产品。
现在我不得不再增加一个步骤。查找特定客户的最后订单(按日期排序)。我尝试了以下谓词,但它不工作:
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"(ANY productOrders.order.client.code == %@ AND [email protected])", clientCode];
其中orderDate
为NSDate
类型。
我必须使用SUBQUERY吗?我怎样才能做到这一点?先谢谢你。
当您只需要一条记录时,您确实可以使用谓词。 – ggfela 2012-02-22 10:58:37