Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
jean-marie castellucci
4,954 PointsSwift + Parse.com : make a follower/following system with join table
Hello
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 seted 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 = "kiki@kiki.com";
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
jean-marie castellucci
4,954 Pointsok i have the solution, to get the detail i just had to query for the user using the getObjectInBackgroundWithId :
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()
})
/* self.followingUserList.addObject(user)
println("User added to following user list : \(user)")
println("followingUserlist = \(self.followingUserList)")*/
} } else {
// Log details of the failure
println("error loadind user ")
println(error)
}
}
}