我正在使用queryWithSubqueries来施加多个约束,如下所示:
func getPosts() {
let getPostsQuery = PFQuery(className: "allPosts")
let getRightPostsQuery = PFQuery(className: "allPosts")
getPostsQuery.whereKey("userId", equalTo: userId) //userId is a string var storing a specific userId
getRightPostsQuery.whereKey("isItRight", equalTo: true)
let combinedQuery = PFQuery.orQueryWithSubqueries([getPostsQuery, getRightPostsQuery])
combinedQuery.limit = 15
combinedQuery.orderByDescending("createdAt")
combinedQuery.findObjectsInBackgroundWithBlock { (posts, error) -> Void in
if let posts = posts {
self.postId.removeAll(keepCapacity: true) //postId is an array to store the postIds
for post in posts {
self.postId.append(post.objectId!!)
}
}
self.tableView.reloadData()
}
}
我正在尝试按照用户ID的描述获取用户发布的所有正确帖子。我使用未发布任何内容的userId测试了查询。奇怪的是,返回的对象不属于该特定用户。然后我尝试使用已发布内容的userId并且还检索了“错误”帖子(isItRight == false),任何想法?感谢
答案 0 :(得分:0)
为什么要这样做并使用合并查询?
getPostsQuery.whereKey("userId", equalTo: userId) //userId is a string var storing a specific userId
getRightPostsQuery.whereKey("isItRight", equalTo: true)
尝试这样做
getPostsQuery.whereKey("userId", equalTo: userId) //userId is a string var storing a specific userId
getPostsQuery.whereKey("isItRight", equalTo: true)
正如Parse Docs所说,"你可以给出多个约束,如果对象匹配所有约束,它们将只在结果中。换句话说,它就像一个约束的AND。"
如果需要,还有另一种方法可以向查询添加约束
// Using NSPredicate
let predicate = NSPredicate(format:"userId == '\(userId)' AND isItRight == 1")
let query = PFQuery(className: "allPosts", predicate: predicate)