parsing“orderByDescending”查询

我有两个名为FollowersPosts类,我正在尝试使主屏幕,我将能够看到用户我下面的post,我的post太..所以我试图让这个查询post:

 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 { } } }) } } } } 

使用这个代码,我得到一个随机顺序的职位。 我在这里错在哪里..如果你需要任何关于我可以给你的问题的解释,但帮助我在​​这里..即时通讯无法find错误。 所以基本上:

  1. 我想根据时间获得orderByDescendingpost。

  2. 在这里,我正在获取当前用户正在跟踪的用户的post,但当前用户的post。 我该怎么办?

首先,我是Android开发人员,不要做iOS,但是我想我得到了你想做的事情,所以请把它翻译一下。 你会想看看ParseQuery.whereMatchesQuery()。 基本上你会有2个查询:一个是主要查询的post,另一个是追随者。

 // 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... 

所以这里是完整的答案..

我知道这不是最优化的代码(你可以重构它的方法),但它给你想要的结果…

 --- Sorted posts by current user Optional(Test) Optional(Test1) Optional(Test2) Optional(Test3) Optional(Test4) Optional(Test20) --- Sorted posts by every follower Optional(Test10) Optional(Test11) Optional(Test12) 

完整的代码在这里:

 override func viewDidLoad() { super.viewDidLoad() if PFUser.currentUser() != nil { PFUser.logOut() } PFUser.logInWithUsernameInBackground("test", password: "test") { (let currentUser, let error) -> Void in println("---") // Insert test post // let testPost = PFObject(className: "Posts") // testPost["post"] = "Test20" // let userRelation : PFRelation = testPost.relationForKey("postedBy") // userRelation.addObject(PFUser.currentUser()!) // testPost.saveEventually() // Insert test relation // let userToFollow = PFUser.query() // userToFollow?.whereKey("username", equalTo: "test1") // userToFollow?.findObjectsInBackgroundWithBlock({ (let result, let error) -> Void in // // if let follower = result!.first! as? PFUser { // // let followers = PFObject(className: "Followers") // let userRelation : PFRelation = followers.relationForKey("user") // userRelation.addObject(PFUser.currentUser()!) // let followerRelation : PFRelation = followers.relationForKey("follower") // followerRelation.addObject(follower) // // followers.saveEventually() // } // }) // Get posts by currentUser let postsByCurrentUser = PFQuery(className: "Posts") postsByCurrentUser.orderByAscending("createdAt") postsByCurrentUser.whereKey("postedBy", equalTo: PFUser.currentUser()!) postsByCurrentUser.findObjectsInBackgroundWithBlock({ (let pfPosts, let error) -> Void in if let posts = pfPosts as? [PFObject] { for post in posts { println(post["post"]) } } }) // Get posts by followers let postsByCurrentUserFollowers = PFQuery(className: "Followers") postsByCurrentUserFollowers.whereKey("user", equalTo: PFUser.currentUser()!) postsByCurrentUserFollowers.findObjectsInBackgroundWithBlock({ (let pfFollowers, let error) -> Void in if let followers = pfFollowers as? [PFObject] { for follower in followers { let userFollowerRelation = follower.relationForKey("follower") userFollowerRelation.query()!.findObjectsInBackgroundWithBlock({ (let followerToUser, let error) -> Void in if let followerUser = followerToUser!.first! as? PFUser { let postsByCurrentUser = PFQuery(className: "Posts") postsByCurrentUser.orderByAscending("createdAt") postsByCurrentUser.whereKey("postedBy", equalTo: followerUser) postsByCurrentUser.findObjectsInBackgroundWithBlock({ (let pfPosts, let error) -> Void in if let posts = pfPosts as? [PFObject] { for post in posts { println(post["post"]) } } }) } }) } } }) } }