Search code examples
swiftxcodeparse-platformtableview

Swift Parse Query For Users That I Follow


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


Solution

  • 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.