2015-08-28 83 views
0

我有两个类叫做FollowersPosts。像这样:whereKey:matchesQuery:没有给出任何结果

Follower =(user , follower),用户和追随者都包含Pointers

Posts = (post, postedBy),这里postedBy包含Pointers。我想要检索当前用户自己发布的帖子posted,并且发布的帖子是当前用户所关注的用户。我想这:

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()!) 
    var compoundQuery = PFQuery.orQueryWithSubqueries([postQuery, postQueryCurrentUser]) 
     compoundQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in 

      if let objects = objects { 

       for object in objects { 

        self.data.addObject(object) 
        println(self.data) 

       } 

      } 

    } 
} 

所以我应该怎么做到这一点呢?感谢您的时间..

回答

0

因此,这里是完整的答案..

我知道这是不是最优化的代码(u能重构它到方法),但它给u想结果...

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

            } 

           }) 


          } 

         }) 



        } 
       } 
      }) 

     } 

    } 
+0

像什么?对不起,初学者在这里。 –

+0

我编辑了我的帖子..ü没有运行搜索的第一个查询任何地方,所以它没有给你任何结果...你必须在搜索搜索,像我张贴在编辑后 –

+0

,给我随机顺序的帖子.. –

0

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

创建一个额外的2个查询,以便总将有四个:

  1. 找到所有的追随者(queryFollowers)
  2. 查找追随者的所有帖子(postQuery)
  3. 查找当前用户所有帖子
  4. 合并由化合物查询2和3查询NEW

    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 }];

我不知道有关语法。但我希望你明白这一点。也请考虑接受我对你昨天的问题的回答。 =)

+0

这给我只是当前用户的帖子:/ –

+0

你能更新你的代码吗? –

+0

我已更新我的代码..你可以现在检查它 –