0

I currently have a query that returns a table of all the users that I have on my server along with a checkmark next to their name if I follow them. Here's the code:

//REFRESHER
func refresh() {

    self.usernames.removeAll()
    self.userIDs.removeAll()
    self.isFollowing.removeAll()


    let query = PFUser.query()

    query?.findObjectsInBackground(block: { (objects, error) in

        if error != nil {
            print(error)

        } else if let users = objects {

            //self.usernames.removeAll()
            //self.userIDs.removeAll()
            //self.isFollowing.removeAll()

            for object in users {

                if let user = object as? PFUser {

                    if user.objectId != PFUser.current()?.objectId {

                        self.usernames.append(user.username!)
                        self.userIDs.append(user.objectId!)

                        let query = PFQuery(className: "Followers")

                        query.whereKey("follower", equalTo: PFUser.current()?.objectId)
                        query.whereKey("following", equalTo: user.objectId)

                        query.findObjectsInBackground(block: { (objects, error) in

                            if let objects = objects {

                                if objects.count > 0 {

                                    self.isFollowing[user.objectId!] = true

                                } else {

                                    self.isFollowing[user.objectId!] = false

                                }

                                if self.isFollowing.count == self.usernames.count {

                                    self.tableview.reloadData()

                                    self.refresher.endRefreshing()

                                }

                            }

                        })


                    }

                }

            }
            //self.refresher.endRefreshing()

        }


    })

}

I'm trying to return a table with only users that I follow, however I can't seem to get it to work as my Followers class is outside of the PFUser class and thus it's difficult to append the usernames and userID's of users in the Follower's class.

My Followers class has four columns, follower and following which are the respective user ID's of each user, and then followerUsername and followingUsername which are the usernames of the respective users. Any help on this would be much appreciated. Thank you!

UPDATE: I'm now using pointers to reference follower and following in Parse and here's how I'm trying to query the users that I'm following. This implementation isn't working so if anyone could point me in the right direction I'd very much appreciate it!

     func refresh() {

    self.usernames.removeAll()
    self.userIDs.removeAll()
    self.isFollowing.removeAll()


           let query = PFQuery(className: "Followers")

           query.includeKey("following")

           query.whereKey("follower", equalTo: PFUser.current()?.objectId)


           query.findObjectsInBackground(block: { (objects, error) in

                            if error != nil {
                                print(error)

                            } else if let following = objects {

                                for object in following {

                                    if let usersFollowing = object as? PFObject {


           self.usernames.append(usersFollowing["followingUsername"] as! String)
           self.userIDs.append(usersFollowing["following"] as! String)


                            if let objects = objects {

                                if objects.count > 0 {


          self.isFollowing[usersFollowing["following"] as! String] = true

                                } else {

          self.isFollowing[usersFollowing["following"] as! String] = false

                                }

          if self.isFollowing.count == self.usernames.count {

                                    self.tableview.reloadData()

                                    self.refresher.endRefreshing()

                                }
                                            }
                                        }

                                }
                            }
    })
}

HERE ARE MY 2 TABLES

userPost Table

Follower Table

1 Answer 1

1

What you have is ridiculously inefficient with the nested queries and table reloads... Once you get any sizable number of users, you're performing way too many queries and hitting that table refresh hard.

You want a table that just has all of the users your current user is following? Your Followers table should use pointers instead of object ids, then you can just use the includeKeys method on queries to have it fetch the following field of the Followers object, and find all objects where follower is equal to the current user.

With the IDs, you could get away with doing a matchesKeyInQuery method, with a Followers query where follower equals the current user's ID, and the User query has matchesKeyInQuery where the key is objectId, queryKey is following, and query is the Follower query. But pointers are the proper way to do join tables, not storing Ids.

You could also look into Relations, which may be a good fit for tracking users that you follow.

Sign up to request clarification or add additional context in comments.

19 Comments

Okay well I'm definitely gonna need efficiency since I'm expecting to have a substantial user base. I'm not very familiar with pointers so could you give me an idea of how I need to change my code to accomplish this most efficiently? I'll be implementing this follower/following in multiple places on the app so it's important that I get it right. You've helped answer some of my questions before so thank you so much!
I set up pointers in my Parse backend for followers and following, however I can't get it to save the currentuserID and following UserID to my pointers in parse when a user is tapped in my table.
Are you trying to assign a string to the value? Create a shell pointer. PFUser* following = [PFUser objectWithoutDataWithObjectId:followingId]; PFUser *follower = [PFUser currentUser];
I just need it to save the objectID of the current user as well as the user at [indexpath.row] which should just be strings. It saved perfectly when I had strings instead of pointers as the follower and following columns in Parse, but now doesn't do anything as I've changed them to pointers
I figured saving and deleting pointers now, my new UPDATE above shows how I'm trying to query only the users that I follow with pointers now. It still isn't working so is there any way you can point out how I might get this to work?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.