0

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

    })




}
Shawn Baek
  • 1,928
  • 3
  • 20
  • 34
  • I dont know what you are trying to achieve here but I would certainly create a Custom Class (or Struct) that holds all your information about the PFObject, creating 5 arrays that will have the same length is really not practical... or just use array of PFObjects – Mazel Tov Aug 27 '16 at 08:18
  • Thanks for the reply. Um My problem is ...I'm trying to retrive data via PFQuery. The data is stored different parse class. But It is related. Get "comment" data from "posts". - find the my fans posts - find the comment about my fans posts ​ My problem is commentsArray. ​ As you can see... commentsArray is not synchronized other arrays (e.g usernameArrays) ​ I want get all the data arrays after that set the uilabel's text. ​ – Shawn Baek Aug 27 '16 at 14:35
  • I also had trying to parse's synchronized method. but It is really slow and got an measage "Warning: A long-running Parse operation is being executed on the main thread" – Shawn Baek Aug 27 '16 at 14:35
  • 1
    first: you shouldnt use countObjectInBackground, Parse doesnt recommend that and it will give you crapy result when you have a lot of object in database.... second, network request is not cheap, you are doing for loop for every object that you downloaded and doing another two network requests, this is very poor design, I would redesign your classes first... – Mazel Tov Aug 27 '16 at 15:32
  • Thank you so much I understand :) It really help for me – Shawn Baek Aug 27 '16 at 23:45

0 Answers0