检查查询键是否为 nil Parse
Check if query key is nil Parse
所以,我在解析中有一个查询,它必须根据键是否等于某个对象来查找对象。这是几行代码。
var gamesQuery = PFQuery(className: "Games")
gamesQuery.whereKey("challenged", equalTo: (PFUser.currentUser()!.objectId!))
gamesQuery.whereKey("challenger", equalTo: PFUser.currentUser()!)
但是,当此查询为运行时,由于没有符合搜索参数的对象,偶尔会发现该查询为nil。
有没有我可以 运行 检查是否
gamesQuery.whereKey("challenged", equalTo: (PFUser.currentUser()!.objectId!))
是零吗?对此问题的任何帮助将不胜感激。
您当前的查询本质上是 and
- "find objects where challenged==current user and challenger==current user".
我认为您正在寻找 or
查询 - "find objects where challenged==current user or challenger==current user"。您可以在 Parse -
中使用 compound query 执行此操作
let challengedQuery = PFQuery(className: "Games")
challengedQuery.whereKey("challenged", equalTo: (PFUser.currentUser()!.objectId!))
let challengerQuery = PFQuery(className: "Games")
challengerQuery.whereKey("challenger", equalTo: PFUser.currentUser()!)
let gamesQuery = PFQuery.orQueryWithSubqueries([challengedQuery, challengerQuery])
gamesQuery.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// results contains challenger and challenged games.
}
}
所以,我在解析中有一个查询,它必须根据键是否等于某个对象来查找对象。这是几行代码。
var gamesQuery = PFQuery(className: "Games")
gamesQuery.whereKey("challenged", equalTo: (PFUser.currentUser()!.objectId!))
gamesQuery.whereKey("challenger", equalTo: PFUser.currentUser()!)
但是,当此查询为运行时,由于没有符合搜索参数的对象,偶尔会发现该查询为nil。
有没有我可以 运行 检查是否
gamesQuery.whereKey("challenged", equalTo: (PFUser.currentUser()!.objectId!))
是零吗?对此问题的任何帮助将不胜感激。
您当前的查询本质上是 and
- "find objects where challenged==current user and challenger==current user".
我认为您正在寻找 or
查询 - "find objects where challenged==current user or challenger==current user"。您可以在 Parse -
let challengedQuery = PFQuery(className: "Games")
challengedQuery.whereKey("challenged", equalTo: (PFUser.currentUser()!.objectId!))
let challengerQuery = PFQuery(className: "Games")
challengerQuery.whereKey("challenger", equalTo: PFUser.currentUser()!)
let gamesQuery = PFQuery.orQueryWithSubqueries([challengedQuery, challengerQuery])
gamesQuery.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// results contains challenger and challenged games.
}
}