用Nsexpression崩溃而不是其谓词建造的嵌套子征服

发布于 2025-01-26 05:50:31 字数 1928 浏览 2 评论 0原文

我有一些代码可以递归地使用nsComparisonPredicatensexpression。这是平坦的时候:

    // Nested subquery
    let entityPath = NSExpression(forKeyPath: "$s.mainUserResult.operations")
    let subQueryExpression = NSExpression(forSubquery: entityPath, usingIteratorVariable: "t", predicate: NSPredicate(format: "$t.operationType == 3"))
    let countSubQuery = NSExpression(format: "%@.@count", argumentArray: [subQueryExpression])
    let p1 = NSComparisonPredicate(leftExpression: countSubQuery,
                                 rightExpression: NSExpression(forConstantValue: 0),
                                 modifier: .direct,
                                 type: .greaterThan,
                                 options: NSComparisonPredicate.Options(rawValue: 0))

    // Main subquery
    let entityPath2 = NSExpression(forKeyPath: "sessions")
    let subQueryExpression2 = NSExpression(forSubquery: entityPath2, usingIteratorVariable: "s", predicate: p1)
    let countSubQuery2 = NSExpression(format: "%@.@count", argumentArray: [subQueryExpression2])
    
    let p2 = NSComparisonPredicate(leftExpression: countSubQuery2,
                                 rightExpression: NSExpression(forConstantValue: 0),
                                 modifier: .direct,
                                 type: .greaterThan,
                                 options: NSComparisonPredicate.Options(rawValue: 0))

上面的代码崩溃时,当我获取以下错误的某些实体时:

由于未被发现的例外,终止应用程序 “ nsinvalidargumentException”,原因:'不能具有无关关系 子Quemysubquery中的收集元件($ S.Mainuserresult. $ t,$ t.operationType == 3)'

$ t ,

用谓词p2获取错误,而使用结果格式进行获取:

   let p2 = NSPredicate(format: "SUBQUERY(sessions, $s, SUBQUERY($s.mainUserResult.operations, $t, $t.operationType == 3).@count > 0).@count > 0")

如何使nscomparisonPredicate工作?

I have some code that can recursively build nested subqueries with NSComparisonPredicate and NSExpression. Here it is when it's flat:

    // Nested subquery
    let entityPath = NSExpression(forKeyPath: "$s.mainUserResult.operations")
    let subQueryExpression = NSExpression(forSubquery: entityPath, usingIteratorVariable: "t", predicate: NSPredicate(format: "$t.operationType == 3"))
    let countSubQuery = NSExpression(format: "%@.@count", argumentArray: [subQueryExpression])
    let p1 = NSComparisonPredicate(leftExpression: countSubQuery,
                                 rightExpression: NSExpression(forConstantValue: 0),
                                 modifier: .direct,
                                 type: .greaterThan,
                                 options: NSComparisonPredicate.Options(rawValue: 0))

    // Main subquery
    let entityPath2 = NSExpression(forKeyPath: "sessions")
    let subQueryExpression2 = NSExpression(forSubquery: entityPath2, usingIteratorVariable: "s", predicate: p1)
    let countSubQuery2 = NSExpression(format: "%@.@count", argumentArray: [subQueryExpression2])
    
    let p2 = NSComparisonPredicate(leftExpression: countSubQuery2,
                                 rightExpression: NSExpression(forConstantValue: 0),
                                 modifier: .direct,
                                 type: .greaterThan,
                                 options: NSComparisonPredicate.Options(rawValue: 0))

The above code crashes when I fetch some entities with the following error:

Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'Can't have a non-relationship
collection element in a subquerySUBQUERY($s.mainUserResult.operations,
$t, $t.operationType == 3)'

What's really weird is that when I build a predicate by hand by copying the resulted predicateFormat of the NSComparisonPredicate, it actually works!

Fetching with the predicate p2 results in an error, whereas fetching with the resulted format works:

   let p2 = NSPredicate(format: "SUBQUERY(sessions, $s, SUBQUERY($s.mainUserResult.operations, $t, $t.operationType == 3).@count > 0).@count > 0")

How to make the NSComparisonPredicate work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記柔刀 2025-02-02 05:50:31

nsexpression(forkeypath:“ $ s.mainuserresult.operations”)

die

self.value(forkeypath:“ $ s.mainuserresult.operations operations'')

这应该是

s s .value(forkeypath:“ mainuserresult.operations”)

表达式:

nsexpression(格式:“ $ s.mainuserresult.operations”,grognnArray:[]))>

NSExpression(forKeyPath: "$s.mainUserResult.operations")

does

self.value(forKeyPath: "$s.mainUserResult.operations")

This should be

s.value(forKeyPath: "mainUserResult.operations")

expression:

NSExpression(format: "$s.mainUserResult.operations", argumentArray: [])

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文