【问题标题】:Result of Core Data query does not change when using a predicate to filter使用谓词过滤时,Core Data 查询的结果不会改变
【发布时间】:2014-12-29 06:32:24
【问题描述】:

我正在用 Objective-C 构建一个 Core Data 项目。我有三个带有名称的表:CountryArea、ChargeStation 和 PlugType。它们有以下关系。

CountryArea>ChargeStation>PlugType

Data:
area1<-->>chargestation1.hasPlugType<-->type1 && -->type2
area1<-->>chargestation2.hasPlugType<-->type1 && -->type3
area1<-->>chargestation3.hasPlugType<-->type2

我正在从表 CountryArea 中获取,该表与 ChargeStation 具有一对多关系,而 ChargeStation 与 PlugType 具有一对多关系。我正在尝试排除所有例如包含 PlugType.type == 'type2' 但如果它们有多种类型的 ChargeStations,则不应使用此谓词排除它们: 只有 station3 应该被排除!

[NSPredicate predicateWithFormat:@"SUBQUERY(stations, $C, SUBQUERY($C.hasPlugType, $T,$T.type != %@).@count).@count > 0",@"type2"];

这会返回与我没有使用谓词相同的结果。

有什么帮助吗?

【问题讨论】:

    标签: ios cocoa-touch core-data nspredicate


    【解决方案1】:

    您希望排除所有具有 PlugType.type = "type" 的 ChargeStation,或者等效地“仅包括那些 (PlugTypes with type = "type2") 计数为零的 ChargeStation”。所以试试:

    [NSPredicate predicateWithFormat:@"SUBQUERY(stations, $C, ANY $C.hasPlugType.type = %@).@count = 0",@"type2"];
    

    【讨论】:

    • 我尝试过:'@"SUBQUERY(stations, $C, ANY $C.hasPlugType.type == %@).@count == 0",@"type2"];'并带有:'@"SUBQUERY(stations, $C, ANY $C.hasPlugType.type != %@).@count > 0",@"type2"];'第一个删除列表中的所有电台,第二个没有任何区别。
    • 您能否编辑您的问题以提供一些数据示例,包括您希望排除的数据?谢谢
    • 我希望这会有所帮助...感谢您的帮助!
    • 好的。要检查,您实际上是在获取 CountryArea 对象-因此您希望包含“area1”,因为它具有chargestation1 和chargestation2。但是如果它只有chargestation3,它应该被排除在外吗?
    • 我想要所有 area1 对象,其站点的插头类型不是 type2。但如果他们有类型 2 和另一种类型的插头,它仍然应该包括在内。在示例中,我想要 station1 和 station2 而不是 station3。
    猜你喜欢
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    相关资源
    最近更新 更多