whereKey:matchesQuery:没有给出任何结果

时间:2015-08-28 06:21:23

标签: ios objective-c swift parse-platform

我有两个名为lxml==3.4.4Followers的课程。像这样的东西:

Posts,用户和关注者都包含Follower =(user , follower)

Pointers,此处发布的内容包含Posts = (post, postedBy)。我想要检索当前用户自己的帖子Pointers以及当前用户所关注的用户发布的帖子。我正在尝试这个:

posted

那我该怎么做才能实现呢?谢谢你的时间..

2 个答案:

答案 0 :(得分: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"])
                                        }

                                    }

                                })


                            }

                        })



                    }
                }
            })

        }

    }

答案 1 :(得分:0)

再次是我。我不是iOS开发人员,我做Android但我熟悉Parse API。您应该阅读关于compund查询的Parse iOS开发人员指南。

创建另外2个查询,因此总计将为4个:

  1. 查找所有关注者(queryFollowers)
  2. 查找关注者发布的所有帖子(postQuery)
  3. 按当前用户
  4. 查找所有帖子
  5. 通过复合查询 NEW

    组合查询2和3

    func loadData(){

    var queryFollowers:PFQuery = PFQuery(className: "Followers")
        queryFollowers.whereKey("follower", equalTo: PFUser.currentUser()!)
    var postQuery:PFQuery = PFQuery(className: "Posts")
        postQuery.whereKey("postedBy", matchesQuery: queryFollowers)
    var postQueryCurrentUser:PFQuery = PFQuery(className: "Posts")
        postQueryCurrentUser.whereKey("postedBy", equalTo:PFUser.currentUser())
    PFQuery *compoundQuery = [PFQuery orQueryWithSubqueries:@[postQuery,postQueryCurrentUser]];
    

    [compoundQuery findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) { // results contains posts from current user and followers }];

  6. 我不确定语法。但我希望你明白这个主意。另外请考虑接受我对你昨天问题的回答。 =)