使用 NSExpression 构建的嵌套子查询崩溃但不是它的谓词
Nested subqueries built with NSExpression crashes but not its predicate
我有一些代码可以使用 NSComparisonPredicate
和 NSExpression
递归构建嵌套子查询。这是平坦的时候:
// 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))
当我获取一些实体时,上述代码崩溃并出现以下错误:
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)'
真正奇怪的是,当我通过复制 NSComparisonPredicate 的结果 predicateFormat
手动构建谓词时,它确实有效!
使用谓词 p2
获取会导致错误,而使用结果格式获取则有效:
let p2 = NSPredicate(format: "SUBQUERY(sessions, $s, SUBQUERY($s.mainUserResult.operations, $t, $t.operationType == 3).@count > 0).@count > 0")
如何使 NSComparisonPredicate
工作?
NSExpression(forKeyPath: "$s.mainUserResult.operations")
会
self.value(forKeyPath: "$s.mainUserResult.operations")
这应该是
s.value(forKeyPath: "mainUserResult.operations")
表达式:
NSExpression(format: "$s.mainUserResult.operations", argumentArray: [])
我有一些代码可以使用 NSComparisonPredicate
和 NSExpression
递归构建嵌套子查询。这是平坦的时候:
// 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))
当我获取一些实体时,上述代码崩溃并出现以下错误:
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)'
真正奇怪的是,当我通过复制 NSComparisonPredicate 的结果 predicateFormat
手动构建谓词时,它确实有效!
使用谓词 p2
获取会导致错误,而使用结果格式获取则有效:
let p2 = NSPredicate(format: "SUBQUERY(sessions, $s, SUBQUERY($s.mainUserResult.operations, $t, $t.operationType == 3).@count > 0).@count > 0")
如何使 NSComparisonPredicate
工作?
NSExpression(forKeyPath: "$s.mainUserResult.operations")
会
self.value(forKeyPath: "$s.mainUserResult.operations")
这应该是
s.value(forKeyPath: "mainUserResult.operations")
表达式:
NSExpression(format: "$s.mainUserResult.operations", argumentArray: [])