How do I retrieve data by using the Parse PFQuery?

85 Views Asked by At

Hi I'm trying to retrive data by using the PFQuery.

loadPosts in the tabelviewController.

My problem is commentsArray.

As you can see... commentsArray is not synchronized other arryas.

Because It has different db's class.

I want get all the data arrays to set the uilabel's text synchronizing

How do I achieve that?

I also had trying to parse's synchronized method. but It is really slow.

Here is my code. This function in the 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

There are 0 best solutions below