解析"orderByDescending".询问 [英] Parse "orderByDescending" query

查看:73
本文介绍了解析"orderByDescending".询问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个叫FollowersPosts的类,我试图制作主屏幕,通过它我可以看到我关注的用户的帖子,也可以看到我的帖子..所以我尝试此操作以获取帖子查询:

I've two classes called Followers and Posts , I am trying to make the home screen by which i will be able to see the posts by the users i am following, and my posts too.. so i tried this to get the query of 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)给出了完美的结果:

here postQuery.orderByDescending("createdAt") is working fine.. println(object.createdAt) gives perfect result :

  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)

如果我也尝试查询关注的用户,就像这样:

and if i try to query followed users too , like this :

  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 {

                            }

                        }

                    })

                }

            }

    }
}

使用此代码,我得到帖子的随机顺序. 我在这里错在哪里..如果您需要任何有关该问题的解释,我可以给您,但请在这里帮助我..我找不到错误.基本上是这样:

using this code i'm getting a random order of the posts. where am i wrong here.. if you need any explanation about the question i can give you but help me out here.. i m not able to find the error. so basically :

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

  1. I want to get the posts in orderByDescending according to the time.

在这里,我正在获取用户的帖子,当前用户正在关注该帖子,但当前用户正在关注该帖子.我该怎么办?

Here i am getting the posts by the users which are being followed by the current user but the current user's posts. What should i do for that ?

推荐答案

所以这是完整的答案..

So here is complete answer ..

我知道这不是最理想的代码(您可以将其重构为方法),但是它可以给您想要的结果...

I know it isn't the most optimal code ( u can refactor it into methods ) but it gives u wanted result ...

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

                                    }

                                })


                            }

                        })



                    }
                }
            })

        }

    }

这篇关于解析"orderByDescending".询问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆