我有两个名为 Followers
和 Posts
的类,我正在尝试制作主屏幕,通过它我可以看到我所在用户的帖子以下,还有我的帖子..所以我尝试这样做来获取帖子的查询:
func loadData() {
var postQuery:PFQuery = PFQuery(className: "Posts")
postQuery.orderByDescending("createdAt")
postQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
for object in objects {
self.data.addObject(object)
println(object.createdAt)
self.tableView.reloadData()
}
}
}
}
这里 postQuery.orderByDescending("createdAt") 工作正常.. println(object.createdAt) 给出了完美的结果:
Optional(2015-08-25 22:31:12 +0000)
Optional(2015-08-25 22:28:28 +0000)
Optional(2015-08-25 22:25:36 +0000)
Optional(2015-08-24 13:40:48 +0000)
Optional(2015-08-24 13:39:25 +0000)
如果我也尝试查询关注的用户,如下所示:
func loadData() {
let postsByCurrentUser = PFQuery(className: "Posts")
postsByCurrentUser.orderByAscending("createdAt")
postsByCurrentUser.whereKey("postedBy", equalTo: PFUser.currentUser()!)
postsByCurrentUser.findObjectsInBackgroundWithBlock ({ (objects, error) -> Void in
if let posts = objects as? [PFObject] {
for post in posts {
}
}
})
var posts:PFQuery = PFQuery(className: "Followers")
posts.whereKey("follower", equalTo: PFUser.currentUser()!)
posts.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
for object in objects {
var followedId = object["user"] as! PFObject
var postsByFollowedUser:PFQuery = PFQuery(className: "Posts")
postsByFollowedUser.whereKey("postedBy", equalTo: followedId)
postsByFollowedUser.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for object in objects {
}
}
})
}
}
}
}
使用此代码我得到了帖子的随机顺序。 我在这里错在哪里..如果您需要有关问题的任何解释,我可以给您,但在这里帮助我..我无法找到错误。所以基本上:
我想根据时间按降序排列帖子。
在这里,我获取当前用户关注的用户的帖子,但当前用户的帖子。我该怎么办?
最佳答案
首先,我是一名 Android 开发者,不做 iOS,但我想我明白你想要做的事情,所以请相应地翻译它。您可能想查看 ParseQuery.whereMatchesQuery()。基本上,您将有 2 个查询:一个针对帖子,这是主要查询,另一个针对关注者。
// Query to find followers of current user
ParseQuery<ParseObject> queryFollowers = ParseQuery.getQuery("Followers");
queryFollowers.whereMatches("follower", currentUser.getId());
// Query to find posts
ParseQuery<ParseObject> postQuery = ParseQuery.getQuery("Posts");
// Retrieves posts which statisfies the first query (followers of current user)
postQuery.whereMatchesQuery("postedBy", queryFollowers);
postQuery.orderByDescending("createdAt");
// Objects returned here are the posts by followers
postQuery.findInBackground...
关于ios - 解析 "orderByDescending"查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32250956/