ios - 解析 "orderByDescending"查询

标签 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. 我想根据时间按降序排列帖子。

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

最佳答案

首先,我是一名 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/

相关文章:

ios - 在UINavigation Controller中获取UITabBarController中View Controller的currentViewController

ios - 对多个项目使用单个 xcode 文件

ios - 如何创建取消按钮?

ios - 在 UIWebview,iOS 中自动播放视频

ios - 长按后 Swift UiTableViewCell 重置

ios - Swift Codable 无法仅自定义所需的按键

swift - 从解析查询后设置跟随按钮标题

javascript - 无法获取指针字段值的值 Parse + React

ios - 从 Facebook 登录获取真实姓名以解析用户

javascript - 从一个 Parse 应用程序在另一个 Parse 应用程序中调用 Cloud Call