如何使用Parse PFQuery检索数据?

时间:2016-08-26 16:42:47

标签: swift parse-platform pfquery pfquerytableviewcontrolle

您好我尝试使用PFQuery来检索数据。

tabelviewController中的

loadPosts。

我的问题是评论阵容。

正如你所看到的...... commentsArray与其他arryas不同步。

因为它有不同的db类。

我想让所有数据数组设置uilabel的文本同步

我如何实现这一目标?

我也试图解析同步方法。但它真的很慢。

这是我的代码。 TableViewController中的这个函数。

func loadPosts() {

    //STEP 1. Find posts related to people who we are following
    let followQuery = PFQuery(className: "fans")

    followQuery.whereKey("myfans", equalTo: PFUser.currentUser()!.username!)
    followQuery.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in

        if error == nil {
            //clean up
            self.followArray.removeAll(keepCapacity: false)

            //Appending where people following..
            //find related objects
            for object in objects! {
                self.followArray.append(object.objectForKey("fan") as! String)
            }
            //append current user to see own posts in feed
            self.followArray.append(PFUser.currentUser()!.username!)


            //STEP 2. Find posts made by people appended to followArray
            let query = PFQuery(className: "posts")
            query.whereKey("username", containedIn: self.followArray)
            query.limit = self.page
            query.addDescendingOrder("createdAt")
            query.findObjectsInBackgroundWithBlock({ (objects:[PFObject]?, error:NSError?) -> Void in

                if error == nil {

                    //clean up
                    self.usernameArray.removeAll(keepCapacity: false)
                    self.profileArray.removeAll(keepCapacity: false)
                    self.dateArray.removeAll(keepCapacity: false)
                    self.postArray.removeAll(keepCapacity: false)
                    self.descriptionArray.removeAll(keepCapacity: false)

                    self.uuidArray.removeAll(keepCapacity: false)

                    //For Test
                    self.commentsArray.removeAll(keepCapacity: false)
                    self.isTappedLikeArray.removeAll(keepCapacity: false)


                    //find related objects
                    for object in objects! {
                        self.usernameArray.append(object.objectForKey("username") as! String)
                        self.profileArray.append(object.objectForKey("profileImg") as! PFFile)
                        self.dateArray.append(object.createdAt)
                        self.postArray.append(object.objectForKey("postImg") as! PFFile)
                        self.descriptionArray.append(object.objectForKey("title") as! String)
                        self.uuidArray.append(object.objectForKey("uuid") as! String)

                        //STEP 3.
                        //Check Comment
                        let countQuery = PFQuery(className: "comments")
                        countQuery.whereKey("to", equalTo: object.objectForKey("uuid") as! String)
                        countQuery.limit = 1
                        countQuery.addAscendingOrder("createdAt")

                        countQuery.countObjectsInBackgroundWithBlock({(count:Int32, error:NSError?) -> Void in



                                //if no any likes are found, else found likes
                                if count==0 {
                                   self.commentsArray.append("")

                                }else{
                                    let commentQuery = PFQuery(className: "comments")

                                    commentQuery.whereKey("to", equalTo: object.objectForKey("uuid") as! String)
                                    commentQuery.limit = 1
                                    commentQuery.addDescendingOrder("createdAt")

                                    commentQuery.findObjectsInBackgroundWithBlock({(objects:[PFObject]?, error:NSError?) -> Void in

                                    if error == nil {
                                        //clean up

                                        //find related object
                                        for object in objects!{


                                            let comment = object.objectForKey("comment") as! String
                                            let by = object.objectForKey("username") as! String


                                            let commentString = by + " " + comment
                                            self.commentsArray.append(commentString)

                                        }


                                        //reload tableView & end spinning of refresher
                                        self.tableView.reloadData()
                                        self.refresher.endRefreshing()

                                    }else {
                                        print(error?.localizedDescription)
                                    }
                                })


                            }
                        })



                    }




                } else {

                    print(error!.localizedDescription)
                }
            })






        } else {
            print(error!.localizedDescription)
        }

    })




}

0 个答案:

没有答案