Swift + Parse.com:如何使用连接表获取用户关系

时间:2014-11-05 09:35:39

标签: ios swift parse-platform pfuser

我有像instagram这样的应用,我必须在用户之间建立关系。 为此,我决定使用连接表方法。 它只是一个名为“Activity”的表,我在其中创建行“fromUser”,“toUser”和活动类型“type”的行,在这种情况下设置为“followingAction”。

我在这里如何设置表格:

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

好的,现在我想获取我当前关注的所有用户(currentUser)并将其显示在tableView中

遵循文档https://parse.com/docs/relations_guide#manytomany-jointables

我做了这个,但它只给了我一个只包含用户objectId的用户数组,我不能拥有在常规用户表中设置的电子邮件名称:

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

如果我打印下面的UserList,这是我放置所提取用户的数组,这就是我所拥有的:

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

当我为常规用户表进行常规查询(PFUser.query())时,我有更详细的信息:

前:

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

在这里,我们可以看到我有完整的PFUser,包括电子邮件,用户名等等。

因为我的“Activity”表中的“fromRow”是指向我的常规User表的指针,为什么我的loadUser()方法中的提取结果不完整?

1 个答案:

答案 0 :(得分:1)

好的,我终于找到了解决方案,我只是使用了像这样的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()

                })


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