0

I have an app like instagram where i have to make relationship between users. For that i decided to use the join table method. It'' s just a table named "Activity" where i created rows "fromUser", "toUser" and a row for the activity type "type" which in this case is set to "followingAction".

Here how i set up the table :

    var activity = PFObject(className:"Activity")

        let currentUser = PFUser.currentUser()

        let follow : String = "followingAction"

        activity.setObject(currentUser, forKey: "fromUser")
        activity.setObject(user, forKey: "toUser") // user is a another PFUser in my app

        activity.setObject(follow, forKey: "type")

        activity.saveEventually()

Ok, now i want to fetch all users that i m currently following (currentUser) and display them in a tableView

Following the doc https://parse.com/docs/relations_guide#manytomany-jointables

i made this but it only give me an array of users which only contain the user objectId, i can't have the email, name which are set up in the regular User table :

       func loadUser () {

    followingUserList.removeAllObjects()

    let findUserObjectId =       PFQuery(className: "Activity")
    findUserObjectId.whereKey("fromUser", equalTo: userPassed)
    findUserObjectId.whereKey("type", equalTo: "followingAction")


    findUserObjectId.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error:NSError!) -> Void in
        if error == nil  {
            // The find succeeded.
            println("succesfully loaded the fromUser  in Activity class")
            // Do something with the found objects
            for object  in objects   {

                let user : PFUser = object["toUser"] as PFUser



                        self.followingUserList.addObject(user)
                        println("User added to following user list : \(user)")
                        println("followingUserlist = \(self.followingUserList)")

                        self.tableView.reloadData()


            } } else {
            // Log details of the failure
            println("error loadind user ")
            println(error)
        }
    }
}

if i print the followingUserList which is the array where i put the fetched users, here is what i have :

followingUserlist = (
"<PFUser: 0x7d9b93c0, objectId: DQJihBpW5E, localId: (null)> {\n}"
)

When i make a regular query (PFUser.query()) for the regular User table, i have more detail :

ex :

   <PFUser: 0x7db5af80, objectId: niwftRrB5x, localId: (null)> {
backgroundImage = "<PFFile: 0x7db5f080>";
email = "[email protected]";
emailVerified = 0;
profileImage = "<PFFile: 0x7db62910>";
username = kiki;
 }

Here we can see i have the complete PFUser with it email, username etc etc.

Because the "fromRow" in my "Activity" table is a pointer to my regular User table, why the fetched result in my loadUser() method isn't complete ?

1 Answer 1

1

Ok i finally found the solution, i just used the getObjectInBackgroundWithId method like this :

   func loadUser () {

    followingUserList.removeAllObjects()

    let findUserObjectId =       PFQuery(className: "Activity")
    findUserObjectId.whereKey("fromUser", equalTo: userPassed)
    findUserObjectId.whereKey("type", equalTo: "followingAction")


    findUserObjectId.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error:NSError!) -> Void in
        if error == nil  {
            // The find succeeded.
            println("succesfully loaded the fromUser  in Activity class")
            // Do something with the found objects
            for object  in objects   {

                let user : PFUser = object["toUser"] as PFUser

                let queryUsers = PFUser.query()
                queryUsers.getObjectInBackgroundWithId(user.objectId, block: { (userGet :PFObject!,error : NSError!) -> Void in
                    self.followingUserList.addObject(userGet)
                    self.tableView.reloadData()

                })


            } } else {
            // Log details of the failure
            println("error loadind user ")
            println(error)
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

is "followingUserList" an array you initialized some where in your code?

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.