用Nsexpression崩溃而不是其谓词建造的嵌套子征服
我有一些代码可以递归地使用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))
上面的代码崩溃时,当我获取以下错误的某些实体时:
由于未被发现的例外,终止应用程序 “ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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: [])