解析" orderByDescending"询问

时间:2015-08-27 13:38:45

标签: ios swift parse-platform

我有两个名为FollowersPosts的课程,我正在尝试制作主屏幕,通过该主屏幕,我可以看到我关注的用户发布的帖子,以及我的帖子帖子也是..所以我试着这个来查询帖子:

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 {

                            }

                        }

                    })

                }

            }

    }
}

使用此代码,我获得了帖子的随机顺序。 我在哪里错了..如果你需要任何关于我可以给你的问题的解释但是在这里帮助我..我无法找到错误。所以基本上:

  1. 我想根据时间在orderByDescending中获取帖子。

  2. 我在这里收到当前用户所关注的用户的帖子,但是当前用户的帖子。我该怎么做?

2 个答案:

答案 0 :(得分:0)

首先,我是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...

答案 1 :(得分:0)

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

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

--- 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"])
                                        }

                                    }

                                })


                            }

                        })



                    }
                }
            })

        }

    }