gpt4 book ai didi

ios - NSPredicate 和核心数据 : retrieve objects among relationships

转载 作者:行者123 更新时间:2023-11-28 20:39:04 27 4
gpt4 key购买 nike

我有一个像下面这样的核心数据模型结构:

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 productOrders.order.@max.orderDate)", clientCode];

其中 orderDateNSDate 类型。

我必须使用子查询吗?我怎样才能做到这一点?提前谢谢你。

最佳答案

您可以在 fetchrequest 上使用 sortDescriptor(fetchrequest 接受一个 sortdescriptors 数组)。

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"productOrders.order.orderDate" ascending:NO];

[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

编辑:见 NeverBe 评论

关于ios - NSPredicate 和核心数据 : retrieve objects among relationships,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9332490/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com